Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update google-logins.md by using Google.Apis.Auth.AspNetCore3 package #33710

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions aspnetcore/security/authentication/social/google-logins.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ uid: security/authentication/google-logins
---
# Google external login setup in ASP.NET Core

By [Valeriy Novytskyy](https://github.com/01binary) and [Rick Anderson](https://twitter.com/RickAndMSFT)
By [Valeriy Novytskyy](https://github.com/01binary) , [Rick Anderson](https://twitter.com/RickAndMSFT) and [Sharaf Abacery](https://github.com/sharafabacery)
sharafabacery marked this conversation as resolved.
Show resolved Hide resolved

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).

## Create the Google OAuth 2.0 Client ID and secret

* Follow the guidance in [Integrating Google Sign-In into your web app](https://developers.google.com/identity/sign-in/web/sign-in) (Google documentation).
* Follow the guidance in [Integrating Google Sign-In into your web app (New)](https://developers.google.com/identity/gsi/web/guides/overview) (Google documentation)
* [Integrating Google Sign-In into your web app (Deprecated)](https://developers.google.com/identity/sign-in/web/sign-in) (Google documentation). (Deprecated)
sharafabacery marked this conversation as resolved.
Show resolved Hide resolved
* Go to [Google API & Services](https://console.cloud.google.com/apis).
* A **Project** must exist first, you may have to create one. Once a project is selected, enter the **Dashboard**.

Expand Down Expand Up @@ -51,7 +52,7 @@ Store sensitive settings such as the Google client ID and secret values with [Se

You can manage your API credentials and usage in the [API Console](https://console.developers.google.com/apis/dashboard).

## Configure Google authentication
## Configure Google authentication (First Edition)
sharafabacery marked this conversation as resolved.
Show resolved Hide resolved

Add the [`Microsoft.AspNetCore.Authentication.Google`](https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.Google) NuGet package to the app.

Expand All @@ -73,7 +74,7 @@ Add the Authentication service to the `Program`:

[!INCLUDE [default settings configuration](includes/default-settings2-2.md)]

## Sign in with Google
## Sign in with Google `Google.Apis.Auth.AspNetCore3` version

* Run the app and select **Log in**. An option to sign in with Google appears.
* Select the **Google** button, which redirects to Google for authentication.
Expand All @@ -85,6 +86,30 @@ Add the Authentication service to the `Program`:

For more information on configuration options supported by Google authentication, see the <xref:Microsoft.AspNetCore.Authentication.Google.GoogleOptions> API reference . This can be used to request different information about the user.

## Configure Google authentication (Second Edition)

Add the [`Google.Apis.Auth.AspNetCore3`](https://www.nuget.org/packages/Google.Apis.Auth.AspNetCore3) NuGet package to the app.


Add the Authentication service to the `program.cs`:

Follow this link [`Add Authtication for asp.net app`](https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#configure-your-application-to-use-google.apis.auth.aspnetcore3)

[!INCLUDE [default settings configuration](includes/default-settings2-2.md)]

## Sign in with Google
* Go to [google developer library link ](https://developers.google.com/identity/gsi/web/guides/client-library) to get link of library.
* Then go to [google developer button genration ](https://developers.google.com/identity/gsi/web/tools/configurator)
* Setup your Controller to match with ` data-login_uri="{HostName}/{ControllerName}/{actionName}" ` attrbute because after success login it will forward you to that link.
* Create controller and action takes one argement `string credential` because that what google return when complete login process.
sharafabacery marked this conversation as resolved.
Show resolved Hide resolved
* Verify `credential` by using this line of code
sharafabacery marked this conversation as resolved.
Show resolved Hide resolved
`GoogleJsonWebSignature.Payload payload = await GoogleJsonWebSignature.ValidateAsync(credential);`
* Here you get all information about login user you can store it in database.
sharafabacery marked this conversation as resolved.
Show resolved Hide resolved

[Complete Simple working app](https://github.com/sharafabacery/GoogleAuthticationExample)
sharafabacery marked this conversation as resolved.
Show resolved Hide resolved

For more information on configuration options supported by Google authentication, see the <xref:Microsoft.AspNetCore.Authentication.Google.GoogleOptions> API reference . This can be used to request different information about the user.

## Change the default callback URI

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.
Expand Down
Loading