aspnetcore/security/authentication/social/google-logins.md
By Valeriy Novytskyy, Rick Anderson, and Sharaf Abacery
This tutorial shows how to enable user sign in with Google accounts using a sample ASP.NET Core project created in xref:security/authentication/social/index. Follow Google's official guidance in Sign in with Google for Web: Setup to create a Google API client ID.
After creating the project, the Dashboard page of the project loads, where it's possible to configure the project.
Open the Credentials tab to create the OAuth client.
The prerequisite to creating the credentials is to configure the OAuth consent screen. If the consent isn't configured, there's a prompt to configure the consent screen.
Create the client credentials for the app by opening the Clients sidebar menu item:
https://localhost:{PORT}/signin-google, where the {PORT} placeholder is the app's port.[!NOTE] The URI segment
/signin-googleis set as the default callback of the Google authentication provider. It's possible to change the default callback URI while configuring the Google authentication middleware via the inherited xref:Microsoft.AspNetCore.Authentication.RemoteAuthenticationOptions.CallbackPath%2A?displayProperty=nameWithType property of the xref:Microsoft.AspNetCore.Authentication.Google.GoogleOptions class.
When deploying the app, either:
Store sensitive settings, such as the Google client ID and secret values, with Secret Manager. For this sample, follow these steps:
Initialize the project for secret storage according to the instructions in xref:security/app-secrets.
Store the sensitive settings in the local secret store with the secret keys Authentication:Google:ClientId (value: {CLIENT ID} placeholder) and Authentication:Google:ClientSecret (value: {CLIENT SECRET} placeholder):
dotnet user-secrets set "Authentication:Google:ClientId" "{CLIENT ID}"
dotnet user-secrets set "Authentication:Google:ClientSecret" "{CLIENT SECRET}"
Manage API credentials and usage in the API Console.
Add the Microsoft.AspNetCore.Authentication.Google nuget package:
dotnet add package Microsoft.AspNetCore.Authentication.Google
:::moniker range=">= aspnetcore-6.0"
Add the authentication service to the Program file:
:::code language="csharp" source="~/security/authentication/social/social-code/6.x/ProgramGoogle.cs" id="snippet1":::
:::moniker-end
:::moniker range="< aspnetcore-6.0"
Add the authentication service to Startup.ConfigureServices:
services.AddAuthentication().AddGoogle(googleOptions =>
{
googleOptions.ClientId = configuration["Authentication:Google:ClientId"];
googleOptions.ClientSecret = configuration["Authentication:Google:ClientSecret"];
});
:::moniker-end
[!INCLUDE default settings configuration]
The user is now logged in using Google credentials.
dotnet/AspNetCore.Docs #14169).ClientSecret in the Google API console.Authentication:Google:ClientId and Authentication:Google:ClientSecret as app settings in the Azure portal. The configuration system is set up to read keys from the environment variables.