How best to implement Google social sign-in authentication in ASP.NET Core?

shertu

I want to implement an authentication system in ASP .NET Core where:

  1. The user clicks a button which looks like the standard Google sign-in button.

  2. The user is then prompted to sign in to Google Accounts and signs in.

  3. A http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier claim with the value equal to that of the user_id of the signed-in Google account is added to the User variable in the RazorBasePage class.

  4. The server adds the user to a user table with user_id as the primary key.

I originally investigated a solution using the built-in ASP .NET Identity system. However, I soon realised it was far more functionality than what I needed.

Next, I followed this article to implement a system where the user must authenticate with their Google account when attempting to use controllers or actions tagged with the [Authorize] attribute.

Meanwhile, I also investigated a login system using this article. This article implements a system where developer can implement their own custom authorisation system, e.g. check against a hard-coded password.

And I also investigated some of Google's developer pages on identity This system allows the developer to easily implement an authentication system on the client side - additional steps are required to pass the authentication to the server.

This collection of images should help to communicate the aforementioned authorisation systems. My current ConfigureServices method in StartUp.cs contains the following code:

services.AddAuthentication(options => {
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
})
    .AddCookie()
    .AddGoogle(options => {
        options.ClientId = Configuration["Authentication:Google:ClientId"];
        options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
        options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.SaveTokens = true;
    });

Any tips on how to implement such a system would be greatly appreciated. Thanks!

Stephen Witherden

Looks like Google deprecated use of Google+ for retrieving user information: https://github.com/aspnet/AspNetCore/issues/6486

In ASP.Net Core MVC 2.0 I ended up doing this in Startup.cs:ConfigureServices

            services.AddAuthentication(options => {
            options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = GoogleDefaults.AuthenticationScheme;
        })
            .AddCookie()
            .AddGoogle(options => {
                options.ClientId = Configuration["Authentication:Google:ClientId"];
                options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
                options.SaveTokens = true;
                options.UserInformationEndpoint = "https://www.googleapis.com/oauth2/v2/userinfo";
                options.ClaimActions.Clear();
                options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
                options.ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
                options.ClaimActions.MapJsonKey(ClaimTypes.GivenName, "given_name");
                options.ClaimActions.MapJsonKey(ClaimTypes.Surname, "family_name");
                options.ClaimActions.MapJsonKey("urn:google:profile", "link");
                options.ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
                options.ClaimActions.MapJsonKey("picture", "picture");
            })
            ;

Don't forget to add the following line before app.UseMvc()

app.UseAuthentication();

Also, you will need to configure Google API for cloud identity for your app.

To display the information you can do something like:

@Context.User.Identity.Name
<img src="@Context.User.Claims.SingleOrDefault(c => c.Type == "picture")?.Value" />

Finally: Please consider privacy of this information. It's not ethical (and in most jurisdictions not legal) to store private information without telling the user that you are storing it and for what purpose.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to migrate asp.net core authentication to new Google SignIn service?

分類Dev

How to implement Permission Based Access Control with Asp.Net Core

分類Dev

How to implement Permission Based Access Control with Asp.Net Core

分類Dev

How to implement permission based authorization in ASP.net core Identity?

分類Dev

ASP.Net Core SAML authentication

分類Dev

ASP.Net Core SAML authentication

分類Dev

ASP.Net Core SAML authentication

分類Dev

ASP.NET Core 2.0 authentication middleware

分類Dev

Windows Authentication with asp.net core

分類Dev

How to redirect after Azure AD authentication to different controller action in ASP Net Core MVC

分類Dev

ASP.NET Core 3 No Sign-In Manager Is Registered for the Scheme

分類Dev

How to implement JWT Refresh Tokens in asp.net core web api (no 3rd party)?

分類Dev

Change authentication from no authentication to individual authentication in existing project of Asp.Net Core (MVC)

分類Dev

How to implement Custom Model Validator in .NET Core

分類Dev

Custom Authentication using legacy user table in Asp.Net Core

分類Dev

Path based authentication in ASP.NET Core MVC 2.0

分類Dev

ASP.Net Core SignalR authentication always responding with 403 - Forbidden

分類Dev

Implement Pagination in ASP.NET Core 2.1 Web API

分類Dev

Use ADFS-based authentication inside ASP.NET Core to access a SQL Server using Windows authentication?

分類Dev

Checking user authentication using Google Sign In and SwiftUI

分類Dev

How to enable CORS in ASP.NET Core

分類Dev

How to implement A/B split testing in ASP.NET?

分類Dev

How to implement ViewModels for asp.net MVC 5?

分類Dev

SPA(Aurelia)+ ASP.NET Core WebAPI + Google認証

分類Dev

Using Azure Active Directory authentication in ASP.NET Core 2.0 from Web App to Web API

分類Dev

ASP.NET core 2.2: what is the expected behaviour of ChallengeResult when there are multiple authentication schemes configured?

分類Dev

ASP.NET Core on .NET 4.6 - how to do https

分類Dev

Store does not implement IUserRoleStore<TUser> ASP.NET Core 2.1 Identity

分類Dev

single sign on for ASP.net and java application

Related 関連記事

  1. 1

    How to migrate asp.net core authentication to new Google SignIn service?

  2. 2

    How to implement Permission Based Access Control with Asp.Net Core

  3. 3

    How to implement Permission Based Access Control with Asp.Net Core

  4. 4

    How to implement permission based authorization in ASP.net core Identity?

  5. 5

    ASP.Net Core SAML authentication

  6. 6

    ASP.Net Core SAML authentication

  7. 7

    ASP.Net Core SAML authentication

  8. 8

    ASP.NET Core 2.0 authentication middleware

  9. 9

    Windows Authentication with asp.net core

  10. 10

    How to redirect after Azure AD authentication to different controller action in ASP Net Core MVC

  11. 11

    ASP.NET Core 3 No Sign-In Manager Is Registered for the Scheme

  12. 12

    How to implement JWT Refresh Tokens in asp.net core web api (no 3rd party)?

  13. 13

    Change authentication from no authentication to individual authentication in existing project of Asp.Net Core (MVC)

  14. 14

    How to implement Custom Model Validator in .NET Core

  15. 15

    Custom Authentication using legacy user table in Asp.Net Core

  16. 16

    Path based authentication in ASP.NET Core MVC 2.0

  17. 17

    ASP.Net Core SignalR authentication always responding with 403 - Forbidden

  18. 18

    Implement Pagination in ASP.NET Core 2.1 Web API

  19. 19

    Use ADFS-based authentication inside ASP.NET Core to access a SQL Server using Windows authentication?

  20. 20

    Checking user authentication using Google Sign In and SwiftUI

  21. 21

    How to enable CORS in ASP.NET Core

  22. 22

    How to implement A/B split testing in ASP.NET?

  23. 23

    How to implement ViewModels for asp.net MVC 5?

  24. 24

    SPA(Aurelia)+ ASP.NET Core WebAPI + Google認証

  25. 25

    Using Azure Active Directory authentication in ASP.NET Core 2.0 from Web App to Web API

  26. 26

    ASP.NET core 2.2: what is the expected behaviour of ChallengeResult when there are multiple authentication schemes configured?

  27. 27

    ASP.NET Core on .NET 4.6 - how to do https

  28. 28

    Store does not implement IUserRoleStore<TUser> ASP.NET Core 2.1 Identity

  29. 29

    single sign on for ASP.net and java application

ホットタグ

アーカイブ