Connect to Identity Providers with Google and OIDC

Tohid haghighi
3 min readApr 16, 2023

--

OpenID Connect

To use Google as an identity provider in .NET Core, you can leverage the OpenID Connect (OIDC) protocol. OIDC is a widely used protocol for authentication and authorization, which is built on top of the OAuth 2.0 authorization framework.

Here are the steps you can follow to configure OIDC with Google in .NET Core:

  1. Create a new ASP.NET Core project in Visual Studio or using the dotnet new command in the terminal.
  2. Install the necessary NuGet packages for OIDC: Microsoft.AspNetCore.Authentication.OpenIdConnect and Microsoft.AspNetCore.Authentication.Google.
  3. In the ConfigureServices method of Startup.cs, add the following code to configure OIDC authentication:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogle(options =>
{
options.ClientId = "your-google-client-id";
options.ClientSecret = "your-google-client-secret";
});
  1. Replace your-google-client-id and your-google-client-secret with the appropriate values for your Google OAuth credentials.
  2. In the Configure method of Startup.cs, add the following code to enable OIDC authentication:
app.UseAuthentication();
app.UseAuthorization();

In your Login action method or wherever you want to initiate the OIDC flow, add the following code to challenge the user using the Google OIDC provider:

public IActionResult Login()
{
return Challenge(new AuthenticationProperties { RedirectUri = "/" }, GoogleDefaults.AuthenticationScheme);
}
  1. Replace "/" with the URL that the user should be redirected to after authentication.
  2. Run your application and navigate to the Login action to start the OIDC flow with Google.

That’s it! Now you have configured OIDC with Google in your .NET Core application.

Connect with OpenIdConnect

To use OpenID Connect (OIDC) with Google as an identity provider in .NET Core, you can follow these steps:

  1. Create a new ASP.NET Core project in Visual Studio or using the dotnet new command in the terminal.
  2. Install the necessary NuGet packages for OIDC: Microsoft.AspNetCore.Authentication.OpenIdConnect.
  3. In the ConfigureServices method of Startup.cs, add the following code to configure OIDC authentication:
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.Authority = "https://accounts.google.com";
options.ClientId = "your-google-client-id";
options.ClientSecret = "your-google-client-secret";
options.CallbackPath = "/signin-google";
options.Scope.Add("openid");
options.Scope.Add("profile");
options.ResponseType = "code";
});
  1. Replace your-google-client-id and your-google-client-secret with the appropriate values for your Google OAuth credentials.
  2. In the Configure method of Startup.cs, add the following code to enable OIDC authentication:
app.UseAuthentication();
app.UseAuthorization();

In your Login action method or wherever you want to initiate the OIDC flow, add the following code to challenge the user using the Google OIDC provider:

public IActionResult Login()
{
return Challenge(new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectDefaults.AuthenticationScheme);
}
  1. Replace "/" with the URL that the user should be redirected to after authentication.
  2. In the ConfigureServices method of Startup.cs, add the following code to handle the callback from Google:
services.AddControllersWithViews();

--

--

Tohid haghighi
Tohid haghighi

Written by Tohid haghighi

Full-Stack Developer | C# | .NET Core | Vuejs | TDD | Javascript

No responses yet