【问题标题】:OWIN - Access External Claims in subsequent requestsOWIN - 在后续请求中访问外部声明
【发布时间】:2016-02-19 01:04:15
【问题描述】:

我有一个 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 样板代码,因此我不会在此处包含它,但如果需要任何特定的内容,我可以添加一些。

感谢您提供的任何帮助。

【问题讨论】:

    标签: c# asp.net oauth oauth-2.0 owin


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多