OWIN-在后续请求中访问外部声明 [英] OWIN - Access External Claims in subsequent requests

查看:86
本文介绍了OWIN-在后续请求中访问外部声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.Net应用程序,该应用程序通过第三方提供商(特别是google)使用OWIN和外部登录.

I have an ASP.Net application that uses OWIN and External logins through a third party provider, specifically google.

在身份验证期间,此代码用于从OwinContext中提取ClaimsIdentity

During Authentication, this code is used to pull the ClaimsIdentity from the OwinContext

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

AuthenticationManager在

where AuthenticationManager is

    private IAuthenticationManager AuthenticationManager
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }

但是,在后续请求(即成功登录后重定向到首页)上,GetExternalLoginInfoAsync()返回null.我要做的是访问有关外部提供商从我的身份验证请求返回的用户帐户(即个人资料图片)的信息.如何从MVC控制器或Web API控制器访问这些声明?

However, on subsequent requests (i.e. redirecting to the home page after successful login) the GetExternalLoginInfoAsync() returns null. What I want to do is access information about the user's account (i.e. profile picture) that the external provider returns from my auth request. How can I access these Claims from an MVC controller or a Web API controller?

我拥有的几乎所有代码都是Visual Studio样板代码,因此这里不包括它,但是如果需要任何特定的内容,我可以添加一些代码.

Almost all of the code I have is Visual Studio boiler plate code, so I won't include it here, but I can add some if anything specific is needed.

感谢您可以提供的任何帮助.

Thanks for any help you can offer.

推荐答案

我试图使用外部登录信息提取图片和个人资料网址等数据.正确的方法是这样将外部来源的版权声明分配给本地身份:

I was trying to use the external login info to pull data such as picture and profile url. The correct way to do this is to assign claims from the external source to the local identity as so:

  public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, string> manager, IAuthenticationManager authentication = null)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here

        if (authentication != null)
        {
            ExternalLoginInfo info = authentication.GetExternalLoginInfo();

            if (info != null)
                foreach (Claim claim in info.ExternalIdentity.Claims.Where(claim => !userIdentity.HasClaim(c => c.Type == claim.Type)))
                {
                    userIdentity.AddClaim(claim);
                    await manager.AddClaimAsync(userIdentity.GetUserId(), claim);
                }
        }

        return userIdentity;
    }

这篇关于OWIN-在后续请求中访问外部声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆