[英]How best to implement Google social sign-in authentication in ASP.NET Core?
我想在ASP .NET Core中實現身份驗證系統,其中:
用戶單擊類似於標准Google登錄按鈕的按鈕。
然后,系統提示用戶登錄Google帳戶並登錄。
在RazorBasePage
類的User
變量中添加了一個http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier
聲明,該聲明的值等於已登錄Google帳戶的user_id
的值。
服務器將用戶添加到以user_id
作為主鍵的用戶表中。
我最初研究了使用內置ASP .NET Identity系統的解決方案。 但是,我很快意識到這比我需要的功能要多得多。
接下來,我將按照本文的內容來實現一個系統,在該系統中,用戶在嘗試使用帶有[Authorize]屬性標記的控制器或操作時必須使用其Google帳戶進行身份驗證。
同時,我還研究了使用本文的登錄系統。 本文實現了一個系統,開發人員可以在該系統中實現自己的自定義授權系統,例如,檢查硬編碼的密碼。
我還研究了Google的一些關於身份的開發人員頁面。該系統使開發人員可以輕松地在客戶端實施身份驗證系統-需要其他步驟才能將身份驗證傳遞給服務器。
圖像的收集應有助於傳達上述授權系統。 我當前在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;
});
任何有關如何實現這樣的系統的技巧將不勝感激。 謝謝!
看起來Google已棄用Google+來檢索用戶信息: https : //github.com/aspnet/AspNetCore/issues/6486
在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" />
最后:請考慮此信息的隱私權。 在不告知用戶您正在存儲隱私以及出於什么目的的情況下存儲隱私信息是不道德的(在大多數司法轄區中也不合法)。
使用打擊代碼獲取用戶數據。
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");
});
您將所有在Callback方法上獲得的數據與Claim
type
和value
一起type
到控制器中。
如果您想除添加鍵之外的其他options.ClaimActions.MapJsonKey(ClaimTypes, jsonKey)
例如options.ClaimActions.MapJsonKey(ClaimTypes, jsonKey)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.