Asp.net mvc 在ASP.NET MVC中使用外部身份验证获取所有服务的用户配置文件照片
在我的ASP.NET MVC中使用Asp.net mvc 在ASP.NET MVC中使用外部身份验证获取所有服务的用户配置文件照片,asp.net-mvc,asp.net-identity,Asp.net Mvc,Asp.net Identity,在我的ASP.NET MVC中使用ExternalLogin时,我无法获取用户的个人资料照片 我在互联网上到处查找,并尝试了不同的方法,例如使用声明例如,给出的答案--似乎我发现的任何东西都过时或不起作用-我还尝试了以下方法来获取个人资料图像(本例中为谷歌) 回到基础,我有以下代码 启动(删除客户端ID和客户端机密 ........ app.UseMicrosoftAccountAuthentication( clientId: "",
ExternalLogin
时,我无法获取用户的个人资料照片
我在互联网上到处查找,并尝试了不同的方法,例如使用声明
例如,给出的答案--似乎我发现的任何东西都过时或不起作用-我还尝试了以下方法来获取个人资料图像(本例中为谷歌)
回到基础,我有以下代码
启动(删除客户端ID和客户端机密
........
app.UseMicrosoftAccountAuthentication(
clientId: "",
clientSecret: "");
app.UseLinkedInAuthentication(
clientId: "",
clientSecret: "");
app.UseFacebookAuthentication(
appId: "",
appSecret: "");
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "",
ClientSecret = ""
});
.......
//获取:/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 });
}
}
[AllowAnonymous]
公共异步任务ExternalLoginCallback(字符串返回URL)
{
var loginInfo=await AuthenticationManager.GetExternalLoginInfoAsync();
if(loginInfo==null)
{
返回重定向操作(“登录”);
}
//如果用户已经登录,请使用此外部登录提供程序登录该用户
var result=await SignInManager.ExternalSignInAsync(loginInfo,isPersistent:false);
开关(结果)
{
案例标志状态成功:
返回重定向到本地(returnUrl);
案例标志状态锁定输出:
返回视图(“锁定”);
案例标志状态。要求验证:
return RedirectToAction(“SendCode”,new{ReturnUrl=ReturnUrl,RememberMe=false});
案例信号状态故障:
违约:
//如果用户没有帐户,则提示用户创建帐户
ViewBag.ReturnUrl=返回URL;
ViewBag.LoginProvider=loginInfo.Login.LoginProvider;
ViewBag.Name=loginInfo.ExternalIdentity.Name;
ViewBag.Avt=loginInfo.ExternalIdentity.Claims.First(a=>a.Type==“urn:google:forfile”);
返回视图(“ExternalLoginConfirmation”,新的ExternalLoginConfirmationViewModel{Email=loginInfo.Email});
}
}
//发布:/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);
}
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务ExternalLoginConfirmation(ExternalLoginConfirmationViewModel模型,字符串返回URL)
{
if(User.Identity.IsAuthenticated)
{
返回重定向操作(“索引”、“管理”);
}
if(ModelState.IsValid)
{
//从外部登录提供程序获取有关用户的信息
var info=await AuthenticationManager.GetExternalLoginInfoAsync();
if(info==null)
{
返回视图(“外部登录失败”);
}
var user=newapplicationuser{UserName=model.UserName,Email=model.Email};
var result=await UserManager.CreateAsync(用户);
if(result.successed)
{
结果=wait UserManager.AddLoginAsync(user.Id,info.Login);
if(result.successed)
{
等待SignInManager.SignInAsync(用户,isPersistent:false,rememberBrowser:false);
返回重定向到本地(returnUrl);
}
}
加法器(结果);
}
ViewBag.ReturnUrl=返回URL;
返回视图(模型);
}
你们如何在一个代码中获得所有服务(Google/Facebook/Linkedin/Microsoft)的个人资料照片
在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");
})
options.UserInformationEndpoint = "https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))";
options.Scope.Add("r_liteprofile");
成功登录后,您可以使用
info.Principal.FindFirstValue("urn:google:picture");
对于Facebook=>
您只需使用标识符
$"https://graph.facebook.com/{identifier}/picture?type=large";
对于LinkedIn=>
在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");
})
options.UserInformationEndpoint = "https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))";
options.Scope.Add("r_liteprofile");
使用这正是我认为正确的实现方式(通过设置适当的范围来请求索赔)。谷歌的工作非常好。