[英]Get User Profile Photo For All Services using External Authentication in ASP.NET MVC
I am unable to get the user's profile photo when using ExternalLogin
in my ASP.NET MVC在 ASP.NET MVC 中使用
ExternalLogin
,我无法获取用户的个人资料照片
I looked up all over the internet and tried different things such as using Claims
for example the answer given here & here - Seems like anything I find is outdated or not working - I also tried the following to get the profile image (google in this case)我在互联网上查找并尝试了不同的方法,例如使用
Claims
,例如此处和此处给出的答案 - 似乎我发现的任何内容都已过时或无法正常工作 - 我还尝试了以下方法来获取个人资料图片(在这种情况下为谷歌)
ViewBag.Image = loginInfo.ExternalIdentity.Claims.First(a=> a.Type == "urn:google:profile");
So going back to basics I have the following code所以回到基础我有以下代码
Startup (removing client IDs and client secrets启动(删除客户端 ID 和客户端机密
........
app.UseMicrosoftAccountAuthentication(
clientId: "",
clientSecret: "");
app.UseLinkedInAuthentication(
clientId: "",
clientSecret: "");
app.UseFacebookAuthentication(
appId: "",
appSecret: "");
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "",
ClientSecret = ""
});
.......
// GET: /Account/ExternalLoginCallback // 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 // 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);
}
How do you guys get the profile photo for all services (Google/Facebook/Linkedin/Microsoft) in one single code?你们如何在一个代码中获得所有服务(Google/Facebook/Linkedin/Microsoft)的个人资料照片?
For Google => in Startup.cs对于 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");
})
and when successful login you can receive the image using成功登录后,您可以使用
info.Principal.FindFirstValue("urn:google:picture");
For Facebook => You just have to use the identifier对于 Facebook => 你只需要使用标识符
$"https://graph.facebook.com/{identifier}/picture?type=large";
For LinkedIn => Use this in Startup.cs对于 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.