为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 ASP.NET Core 中最好地实现 Google 社交登录身份验证?

如何在 ASP.NET Core 中最好地实现 Google 社交登录身份验证?

C#
一只名叫tom的猫 2023-07-09 15:12:11
我想在 ASP .NET Core 中实现一个身份验证系统,其中:用户单击一个看起来像标准 Google 登录按钮的按钮。然后系统会提示用户登录 Google 帐户并登录。http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier其值等于user_id登录的 Google 帐户的声明将添加到类User中的变量中RazorBasePage。服务器将用户添加到用户表中user_id作为主键。我最初研究了使用内置 ASP .NET Identity 系统的解决方案。然而,我很快意识到它的功能远远超出了我的需要。而且我还调查了谷歌的一些关于身份验证的开发者页面。该系统允许开发者轻松地在客户端实现身份验证系统——需要额外的步骤来将身份验证传递到服务器。我当前在 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.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;        options.SaveTokens = true;    });任何有关如何实施此类系统的提示将不胜感激。谢谢!
查看完整描述

2 回答

?
慕桂英546537

TA贡献1848条经验 获得超10个赞

看起来 Google 已弃用 Google+ 来检索用户信息: 

在 ASP.Net Core MVC 2.0 中,我最终在 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");

            })

            ;

不要忘记在 app.UseMvc() 之前添加以下行


app.UseAuthentication();

此外,您还需要为您的应用程序配置 Google API 以获得云身份。


要显示信息,您可以执行以下操作:


@Context.User.Identity.Name

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

最后:请考虑此信息的隐私。在不告诉用户您存储私人信息以及存储目的的情况下存储私人信息是不道德的。


查看完整回答
反对 回复 2023-07-09
?
元芳怎么了

TA贡献1798条经验 获得超7个赞

使用下面的代码获取用户数据。


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;

    options.UserInformationEndpoint = "https://openidconnect.googleapis.com/v1/userinfo";

    options.ClaimActions.Clear();   

    options.ClaimActions.MapJsonKey(ClaimTypes.PPID, "ppid");        

    options.ClaimActions.MapJsonKey(ClaimTypes.Name, "email");

});

您通过回调方法获得的所有数据都通过 inClaim type和进入控制器value。


如果你想进入其他而不是添加你的密钥,比如options.ClaimActions.MapJsonKey(ClaimTypes, jsonKey)


查看完整回答
反对 回复 2023-07-09
  • 2 回答
  • 0 关注
  • 105 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信