[英]Get User Profile Photo For All Services using External Authentication in ASP.NET MVC
在 ASP.NET MVC 中使用ExternalLogin
,我無法獲取用戶的個人資料照片
我在互聯網上查找並嘗試了不同的方法,例如使用Claims
,例如此處和此處給出的答案 - 似乎我發現的任何內容都已過時或無法正常工作 - 我還嘗試了以下方法來獲取個人資料圖片(在這種情況下為谷歌)
ViewBag.Image = loginInfo.ExternalIdentity.Claims.First(a=> a.Type == "urn:google:profile");
所以回到基礎我有以下代碼
啟動(刪除客戶端 ID 和客戶端機密
........
app.UseMicrosoftAccountAuthentication(
clientId: "",
clientSecret: "");
app.UseLinkedInAuthentication(
clientId: "",
clientSecret: "");
app.UseFacebookAuthentication(
appId: "",
appSecret: "");
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "",
ClientSecret = ""
});
.......
// GET: /Account/ExternalLoginCallback
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
// Sign in the user with this external login provider if the user already has a login
var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
case SignInStatus.Failure:
default:
// If the user does not have an account, then prompt the user to create an account
ViewBag.ReturnUrl = returnUrl;
ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
ViewBag.Name = loginInfo.ExternalIdentity.Name;
ViewBag.Avt = loginInfo.ExternalIdentity.Claims.First(a=> a.Type == "urn:google:forfile");
return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
}
}
// POST: /Account/ExternalLoginConfirmation
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction("Index", "Manage");
}
if (ModelState.IsValid)
{
// Get the information about the user from the external login provider
var info = await AuthenticationManager.GetExternalLoginInfoAsync();
if (info == null)
{
return View("ExternalLoginFailure");
}
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
var result = await UserManager.CreateAsync(user);
if (result.Succeeded)
{
result = await UserManager.AddLoginAsync(user.Id, info.Login);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
return RedirectToLocal(returnUrl);
}
}
AddErrors(result);
}
ViewBag.ReturnUrl = returnUrl;
return View(model);
}
你們如何在一個代碼中獲得所有服務(Google/Facebook/Linkedin/Microsoft)的個人資料照片?
對於 Google => Startup.cs
.AddGoogle(options =>
{
var googleAuthNSection =
Configuration.GetSection("Authentication:Google");
options.ClientId = googleAuthNSection["ClientId"];
options.ClientSecret = googleAuthNSection["ClientSecret"];
options.ClaimActions.MapJsonKey("urn:google:picture", "picture", "url");
options.Scope.Add("https://www.googleapis.com/auth/userinfo.profile");
})
成功登錄后,您可以使用
info.Principal.FindFirstValue("urn:google:picture");
對於 Facebook => 你只需要使用標識符
$"https://graph.facebook.com/{identifier}/picture?type=large";
對於 LinkedIn => 在 Startup.cs 中使用它
options.UserInformationEndpoint = "https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))";
options.Scope.Add("r_liteprofile");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.