c# - 如何使用 ASP.Net Core Identity 从登录用户检索 Google 个人资料图片?

标签 c# asp.net-core google-api asp.net-identity google-oauth

好的...我目前正在使用 ASP.Net Core 1.1.2 和 ASP.NET Core Identity 1.1.2。

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

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        //...
        app.UseGoogleAuthentication(new GoogleOptions
        {
            AuthenticationScheme = "Google",
            SignInScheme = "Identity.External", // this is the name of the cookie middleware registered by UseIdentity()
            ClientId = Configuration["ExternalLoginProviders:Google:ClientId"],
            ClientSecret = Configuration["ExternalLoginProviders:Google:ClientSecret"]
        });
    }

GoogleOptions 附带 Microsoft.AspNetCore.Authentication.Google 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);
        string email = info.Principal.FindFirstValue(ClaimTypes.Email);
        string firstName = info.Principal.FindFirstValue(ClaimTypes.GivenName);
        string lastName = info.Principal.FindFirstValue(ClaimTypes.Surname);
        //
    }

所以,到目前为止一切正常。在这里我被困住了。我读了很多关于 accesstokens 和 claims called pictureUrl 等的文章。但是 Principal 不包含任何这些内容。

所以问题是:如何在 ExternalLoginCallback 函数中检索一次个人资料图像?

最佳答案

我在 ASP.NET Core 2.0 上遇到了同样的问题。 有一种更好的方法可以从 startup.cs 中的 OnCreatingTicket 事件中检索图片。在您的情况下,您必须将特定声明“图片” 添加到身份中。

    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddAuthentication()
            .AddCookie()
            .AddGoogle(options =>
            {
                options.ClientId = Configuration["Google.LoginProvider.ClientId"];
                options.ClientSecret = Configuration["Google.LoginProvider.ClientKey"];
                options.Scope.Add("profile");
                options.Events.OnCreatingTicket = (context) =>
                {
                    context.Identity.AddClaim(new Claim("image", context.User.GetValue("image").SelectToken("url").ToString()));

                    return Task.CompletedTask;
                };
            });
    }

然后在您的 AccountController 中,您可以从外部登录信息方法中选择图像。

var info = await _signInManager.GetExternalLoginInfoAsync();

var picture = info.Principal.FindFirstValue("image");

关于c# - 如何使用 ASP.Net Core Identity 从登录用户检索 Google 个人资料图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855503/

相关文章:

google-api - node js 是否使用 googleapis 或 google-cloud 库

c# - 使用 LINQ 从域列表中删除子域

c# - 使用 json 数组和 json 对象的优缺点

c# - 属性的自定义配置绑定(bind)器

c# - 如何使用基于声明的授权保护 asp.net core 2.1 中的静态文件夹

javascript - 使用 javascript 选择融合表时出现错误 401

c# - WPF 虚拟化 Treeview 中的滚动错误

c# - GAC 中的 MySql.Data,仍然是 SecurityException

authentication - 如何将 Windows 身份验证和 JWT 与 .Net Core 2.1 结合使用

python - Google Calendar API - 服务帐户 - 没有 Google Apps 域