ClaimTypes,用于在MVC 6中获取用户的个人资料图像 [英] ClaimTypes for getting the user's profile image in mvc 6
本文介绍了ClaimTypes,用于在MVC 6中获取用户的个人资料图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的 ExternalLoginConfirmation
任务,这是创建项目时添加的默认代码.我添加了以下几行来获得用户的要求:
This is my ExternalLoginConfirmation
task, that is the default code added when the project is created. I have added these lines to get the user claims:
user.Firstname = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName)?.ToString();
user.Lastname = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname)?.ToString();
user.Gender = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Gender)?.ToString();
但是我无法获得用户个人资料图片,因为它在 ClaimTypes
中不存在.
However I'm not able to get the user profile image as it does not exist in ClaimTypes
.
public async Task<IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null)
{
if (ModelState.IsValid)
{
// Get the information about the user from the external login provider
var info = await _signInManager.GetExternalLoginInfoAsync();
if (info == null)
{
return View("ExternalLoginFailure");
}
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await _userManager.CreateAsync(user);
if (result.Succeeded)
{
result = await _userManager.AddLoginAsync(user, info);
if (result.Succeeded)
{
user.Firstname = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GivenName)?.ToString();
user.Lastname = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Surname)?.ToString();
user.Gender = info.Principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Gender)?.ToString();
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider);
return RedirectToLocal(returnUrl);
}
}
AddErrors(result);
}
ViewData["ReturnUrl"] = returnUrl;
return View(model);
}
请注意,缺少 info
的 ExternalPrincipal
,并且以下功能也不可用:
Note that info
is missing ExternalPrincipal
and the following functionality is not available either:
info.ExternalPrincipal.FindFirstValue("pictureUrl");
推荐答案
这是我现在使用的灵魂:
This is the soultion I'm using now:
var result = await _userManager.CreateAsync(user);
if (result.Succeeded)
{
result = await _userManager.AddLoginAsync(user, info);
if (result.Succeeded)
{
if (info.LoginProvider.ToLower().IndexOf("google") != -1)
{ await _userManager.AddClaimAsync(user, new Claim("GooglePlusId", info.ProviderKey));
try {
HttpClient client = new HttpClient();
HttpResponseMessage x = await client.GetAsync($"https://www.googleapis.com/plus/v1/people/{info.ProviderKey}?fields=image&key=YOUR_GOOGLE_PLUS_API_KEY");
dynamic img = Newtonsoft.Json.JsonConvert.DeserializeObject(await x.Content.ReadAsStringAsync());
user.PhotoLink = img.image.url;
db.SaveChanges();
}
catch { }
}
if (info.LoginProvider.ToLower().IndexOf("facebook") != -1)
{
user.PhotoLink = $"http://graph.facebook.com/{info.ProviderKey}/picture?type=square&width=50";
}
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider);
return RedirectToLocal(returnUrl);
}
}
P.S .:此代码应添加到 AccountControllers
控制器
P.S.: this code should be added to ExternalLoginConfirmation
action in AccountControllers
controller
这篇关于ClaimTypes,用于在MVC 6中获取用户的个人资料图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文