我有一个可行的解决方案,但我想知道这是否是正确的方法。这是我到目前为止得到的。
我使用的是ASP.Net Core1.1.2和ASP.NET核心标识1.1.2。
Startup.cs中的重要部分如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//...
app.UseFacebookAuthentication(new FacebookOptions
{
AuthenticationScheme = "Facebook",
AppId = Configuration["ExternalLoginProviders:Facebook:AppId"],
AppSecret = Configuration["ExternalLoginProviders:Facebook:AppSecret"]
});
}FacebookOptionscomes与Microsoft.AspNetCore.Authentication.Facebook nuget套餐。
AccountController.cs中的回调函数如下所示:
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
{
//... SignInManager<User> _signInManager; declared before
ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync();
SignInResult signInResult = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
byte[] thumbnailBytes = null;
if (info.LoginProvider == "Facebook")
{
string nameIdentifier = info.Principal.FindFirstValue(ClaimTypes.NameIdentifier);
string thumbnailUrl = $"https://graph.facebook.com/{nameIdentifier}/picture?type=large";
using (HttpClient httpClient = new HttpClient())
{
thumbnailBytes = await httpClient.GetByteArrayAsync(thumbnailUrl);
}
}
//...
}因此,这段代码运行得非常好,但是,正如前面提到的,这是正确的方法吗--(技术上,不是基于意见的)?
发布于 2018-08-28 12:35:28
要从Facebook获取配置文件图片,您需要配置Facebook选项,并在OnCreatingTicket事件上从OAuth订阅。
services.AddAuthentication().AddFacebook("Facebook", options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.ClientId = configuration.GetSection("ExternalLogin").GetSection("Facebook").GetSection("ClientId").Value;
options.ClientSecret = configuration.GetSection("ExternalLogin").GetSection("Facebook").GetSection("ClientSecret").Value;
options.Fields.Add("picture");
options.Events = new OAuthEvents
{
OnCreatingTicket = context =>
{
var identity = (ClaimsIdentity)context.Principal.Identity;
var profileImg = context.User["picture"]["data"].Value<string>("url");
identity.AddClaim(new Claim(JwtClaimTypes.Picture, profileImg));
return Task.CompletedTask;
}
};
});发布于 2020-01-06 21:38:45
在ASP.NET Core3.0中,OAuthCreatingTicketContext发生了重大变化,参见https://learn.microsoft.com/en-US/dotnet/core/compatibility/2.2-3.0
我改变了
var profileImg = context.User["picture"]["data"].Value<string>("url");至
var profileImg = context.User.GetProperty("picture").GetProperty("data").GetProperty("url").ToString();发布于 2019-10-30 02:45:34
我只使用标识符从图形api中获得图像。
$"https://graph.facebook.com/{identifier}/picture?type=large";https://stackoverflow.com/questions/45855660
复制相似问题