You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/security/authentication/social/google-logins.md
+80-49Lines changed: 80 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,85 +4,116 @@ author: rick-anderson
4
4
description: This tutorial demonstrates the integration of Google account user authentication into an existing ASP.NET Core app.
5
5
ms.author: riande
6
6
ms.custom: mvc
7
-
ms.date: 3/3/2022
7
+
ms.date: 06/18/2025
8
8
uid: security/authentication/google-logins
9
9
---
10
10
# Google external login setup in ASP.NET Core
11
11
12
-
By [Valeriy Novytskyy](https://github.com/01binary), [Rick Anderson](https://twitter.com/RickAndMSFT) and [Sharaf Abacery](https://github.com/sharafabacery)
12
+
By [Valeriy Novytskyy](https://github.com/01binary), [Rick Anderson](https://twitter.com/RickAndMSFT), and [Sharaf Abacery](https://github.com/sharafabacery)
13
13
14
-
This tutorial shows you how to enable users to sign in with their Google account using the ASP.NET Core project created on the [previous page](xref:security/authentication/social/index).
14
+
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](https://developers.google.com/identity/gsi/web/guides/get-google-api-clientid) to create a Google API client ID.
15
15
16
-
## Create the Google OAuth 2.0 Client ID and secret
16
+
## Create the app in Google
17
17
18
-
* Follow the guidance in [Integrating Google Sign-In into your web app](https://developers.google.com/identity/gsi/web/guides/overview) (Google documentation)
19
-
* Go to [Google API & Services](https://console.cloud.google.com/apis).
20
-
* A **Project** must exist first, you may have to create one. Once a project is selected, enter the **Dashboard**.
18
+
* Navigate to the [Google API & Services](https://console.cloud.google.com/apis) page of the Google Cloud platform.
19
+
* If no project exists, create a new project by selecting the **Create Project** button. To select a different project than an existing project that loads, select the loaded project's button in the top-left corner of the UI, followed by the project. To add a new project, select the loaded project's button in the top-left corner of the UI, followed by the **New project** button.
20
+
* When creating a new project:
21
+
* Enter a **Project name**.
22
+
* Optionally, select an **Organization** for the project.
23
+
* Select the **Create** button.
21
24
22
-
* In the **OAuth consent screen** of the **Dashboard**:
23
-
* Select **User Type - External** and **CREATE**.
24
-
* In the **App information** dialog, Provide an **app name** for the app, **user support email**, and **developer contact information**.
25
-
* Step through the **Scopes** step.
26
-
* Step through the **Test users** step.
27
-
* Review the **OAuth consent screen** and go back to the app **Dashboard**.
25
+
After creating the project, the **Dashboard** page of the project loads, where it's possible to configure the project.
28
26
29
-
* In the **Credentials** tab of the application Dashboard, select **CREATE CREDENTIALS** > **OAuth client ID**.
30
-
* Select **Application type** > **Web application**, choose a **name**.
31
-
* In the **Authorized redirect URIs** section, select **ADD URI** to set the redirect URI. Example redirect URI: `https://localhost:{PORT}/signin-google`, where the `{PORT}` placeholder is the app's port.
32
-
* Select the **CREATE** button.
33
-
* Save the **Client ID** and **Client Secret** for use in the app's configuration.
34
-
* When deploying the site, either:
35
-
* Update the app's redirect URI in the **Google Console** to the app's deployed redirect URI.
36
-
* Create a new Google API registration in the **Google Console** for the production app with its production redirect URI.
27
+
Open the **Credentials** tab to create the OAuth client.
28
+
29
+
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.
30
+
31
+
* Select **Configure consent screen** or select **OAuth consent screen** in the sidebar.
32
+
* In the **OAuth consent screen**, select **Get started**.
33
+
* Set the **App name** and **User support email**.
34
+
* Set the audience type to **External**.
35
+
* Add **Contact information** by entering a contact email address.
36
+
* Agree to the terms.
37
+
* Select **Create**.
38
+
39
+
Create the client credentials for the app by opening the **Clients** sidebar menu item:
40
+
41
+
* Select the **Create client** button.
42
+
* Select **Web application** as the **Application type**.
43
+
* Enter a **Name** for the client.
44
+
* Add an **Authorized redirect URI**. For local testing, use the default address `https://localhost:{PORT}/signin-google`, where the `{PORT}` placeholder is the app's port.
45
+
* Select the **Create** button to create the client.
46
+
* Save the **Client ID** and **Client secret**, which are used later in the ASP.NET app configuration.
47
+
48
+
> [!NOTE]
49
+
> The URI segment `/signin-google` is 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.
50
+
51
+
When deploying the app, either:
52
+
53
+
* Update the app's redirect URI in the **Google Console** to the app's deployed redirect URI.
54
+
* Create a new Google API registration in the **Google Console** for the production app with its production redirect URI.
37
55
38
56
## Store the Google client ID and secret
39
57
40
-
Store sensitive settings such as the Google client ID and secret values with [Secret Manager](xref:security/app-secrets). For this sample, use the following steps:
58
+
Store sensitive settings, such as the Google client ID and secret values, with [Secret Manager](xref:security/app-secrets). For this sample, follow these steps:
41
59
42
-
1. Initialize the project for secret storage per the instructions at [Enable secret storage](xref:security/app-secrets#enable-secret-storage).
43
-
1. Store the sensitive settings in the local secret store with the secret keys `Authentication:Google:ClientId` and `Authentication:Google:ClientSecret`:
60
+
1. Initialize the project for secret storage according to the instructions in <xref:security/app-secrets>.
61
+
1. 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):
44
62
45
63
```dotnetcli
46
-
dotnet user-secrets set "Authentication:Google:ClientId" "<client-id>"
47
-
dotnet user-secrets set "Authentication:Google:ClientSecret" "<client-secret>"
64
+
dotnet user-secrets set "Authentication:Google:ClientId" "{CLIENT ID}"
65
+
dotnet user-secrets set "Authentication:Google:ClientSecret" "{CLIENT SECRET}"
You can manage your API credentials and usage in the [API Console](https://console.developers.google.com/apis/dashboard).
70
+
Manage API credentials and usage in the [API Console](https://console.developers.google.com/apis/dashboard).
53
71
54
72
## Configure Google authentication
55
73
56
-
* Add the [`Google.Apis.Auth.AspNetCore3`](https://www.nuget.org/packages/Google.Apis.Auth.AspNetCore3) NuGet package to the app.
57
-
* Add the Authentication service to the `program.cs`:
58
-
* Follow [`Add Authentication for asp.net app`](https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#configure-your-application-to-use-google.apis.auth.aspnetcore3)
Add the authentication service to the `Program` file:
61
77
62
-
## Sign in with Google
63
-
* Get a link to the library at [Google Developer Library](https://developers.google.com/identity/gsi/web/guides/client-library).
64
-
* Then go to [Google Developer Button Generation](https://developers.google.com/identity/gsi/web/tools/configurator).
65
-
* Setup your Controller to match the `data-login_uri="{HostName}/{ControllerName}/{actionName}"` attribute, as it will forward you to that link after a successful login.
66
-
* Create a controller and action that takes one argument `string credential`, which is returned by Google upon completing the login process.
67
-
* Verify the `credential` using the following line of code:
* Under **Use another service to log in.**, select Google.
102
+
* The browser is redirected to **Google** for authentication.
103
+
* Select the Google account to log in or enter Google credentials.
104
+
* If this is the first time signing in, there's a prompt to allow the app to access the Google account information.
105
+
* The browser is redirected back to the app, where it's possible to set the email.
73
106
74
-
The URI segment `/signin-google` is set as the default callback of the Google authentication provider. You can change the default callback URI while configuring the Google authentication middleware via the inherited <xref:Microsoft.AspNetCore.Authentication.RemoteAuthenticationOptions.CallbackPath?displayProperty=nameWithType> property of the <xref:Microsoft.AspNetCore.Authentication.Google.GoogleOptions> class.
107
+
The user is now logged in using Google credentials.
75
108
76
109
## Troubleshooting
77
110
78
-
* If the sign-in doesn't work and you aren't getting any errors, switch to development mode to make the issue easier to debug.
79
-
* If Identity isn't configured by calling `services.AddIdentity` in `ConfigureServices`, attempting to authenticate results in *ArgumentException: The 'SignInScheme' option must be provided*. The project template used in this tutorial ensures Identity is configured.
80
-
* If the site database has not been created by applying the initial migration, you get *A database operation failed while processing the request* error. Select **Apply Migrations** to create the database, and refresh the page to continue past the error.
81
-
* HTTP 500 error after successfully authenticating the request by the OAuth 2.0 provider such as Google: See [this GitHub issue](https://github.com/dotnet/AspNetCore.Docs/issues/14169).
82
-
* How to implement external authentication with Google for React and other SPA apps: See [this GitHub issue](https://github.com/dotnet/AspNetCore.Docs/issues/14169).
111
+
* If the sign-in doesn't work without receiving any errors, switch to development mode to make the app and Google registration easier to debug.
112
+
* If the site's database hasn't been created by applying the initial migration, the following error occurs: *A database operation failed while processing the request*. Select **Apply Migrations** to create the database and refresh the page to continue past the error.
113
+
* For information about an HTTP 500 error after successfully authenticating the request by the OAuth 2.0 provider, such as Google, and information on how to implement external authentication with Google for React and other SPA apps, see [Middleware not handling 'signin-google' route after successful authentication in Asp.Net Core Web Api External Login Authentication (`dotnet/AspNetCore.Docs`#14169)](https://github.com/dotnet/AspNetCore.Docs/issues/14169).
83
114
84
115
## Next steps
85
116
86
-
* This article showed how you can authenticate with Google. You can follow a similar approach to authenticate with other providers listed on the [previous page](xref:security/authentication/social/index).
87
-
* Once you publish the app to Azure, reset the `ClientSecret` in the Google API Console.
88
-
* Set the `Authentication:Google:ClientId` and `Authentication:Google:ClientSecret` as application settings in the Azure portal. The configuration system is set up to read keys from environment variables.
117
+
* This article demonstrates authentication with Google. For information on authenticating with other external providers, see <xref:security/authentication/social/index>.
118
+
*After the app is deployed to Azure, reset the `ClientSecret` in the Google API console.
119
+
* Set the `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.
0 commit comments