繁体   English   中英

OWIN-在后续请求中访问外部声明

[英]OWIN - Access External Claims in subsequent requests

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

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

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

AuthenticationManager在哪里

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

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

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

谢谢你的尽心帮助。

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

  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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM