首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从具有ASP.Net核心身份的登录用户中检索Facebook配置文件图片?

如何从具有ASP.Net核心身份的登录用户中检索Facebook配置文件图片?
EN

Stack Overflow用户
提问于 2017-08-24 07:39:56
回答 5查看 2.7K关注 0票数 5

我有一个可行的解决方案,但我想知道这是否是正确的方法。这是我到目前为止得到的。

我使用的是ASP.Net Core1.1.2和ASP.NET核心标识1.1.2。

Startup.cs中的重要部分如下所示:

代码语言:javascript
运行
复制
    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中的回调函数如下所示:

代码语言:javascript
运行
复制
    [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);
            }
        }
        //...
    }

因此,这段代码运行得非常好,但是,正如前面提到的,这是正确的方法吗--(技术上,不是基于意见的)?

EN

回答 5

Stack Overflow用户

发布于 2018-08-28 12:35:28

要从Facebook获取配置文件图片,您需要配置Facebook选项,并在OnCreatingTicket事件上从OAuth订阅。

代码语言:javascript
运行
复制
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;
        }
    };
});
票数 6
EN

Stack Overflow用户

发布于 2020-01-06 21:38:45

在ASP.NET Core3.0中,OAuthCreatingTicketContext发生了重大变化,参见https://learn.microsoft.com/en-US/dotnet/core/compatibility/2.2-3.0

我改变了

代码语言:javascript
运行
复制
var profileImg = context.User["picture"]["data"].Value<string>("url");

代码语言:javascript
运行
复制
var profileImg = context.User.GetProperty("picture").GetProperty("data").GetProperty("url").ToString();
票数 3
EN

Stack Overflow用户

发布于 2019-10-30 02:45:34

我只使用标识符从图形api中获得图像。

代码语言:javascript
运行
复制
$"https://graph.facebook.com/{identifier}/picture?type=large";
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45855660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档