2 回答

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" />
最后:請考慮此信息的隱私。在不告訴用戶您存儲私人信息以及存儲目的的情況下存儲私人信息是不道德的。

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)
- 2 回答
- 0 關注
- 234 瀏覽
添加回答
舉報