亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 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 關注
  • 234 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號