【问题标题】:Confusion over LOCAL AUTHORITY claims and External Provider claims对 LOCAL AUTHORITY 声明和 External Provider 声明的混淆
【发布时间】:2015-04-29 03:46:48
【问题描述】:

我正在创建一个简单的 WebApi,它允许用户与 Facebook 连接。当我从 facebook 取回 accessToken 时,我正在调用 RegisterExternal 来创建一个 Asp.Net 身份记录并存储来自令牌的声明。这些声明还包括我稍后查询 facebook 图所需的访问令牌。到目前为止,一切似乎都很好。

我遇到的问题是阅读声明。我可以看到它们在我的数据库中,我只是不知道如何查询这些数据。我试过了

var claimsIdentity = User.Identity as ClaimsIdentity;

但这会给我 2 个索赔 a)“http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name” b) 角色

这两个都是颁发者 LOCAL AUTHORITY(老实说,我不确定它们是什么时候创建的,因为我没有明确添加这些)。所以我相信他们要么混淆了我将索赔保存到数据库中而不是错误类型的发行人

await userManager.AddClaimAsync(user.Id, new Claim("urn:facebook:access_token", accessTokenClaim.Value, ClaimValueTypes.String, "LOCAL AUTHORITY"));

或者我访问声明的代码不正确。

有人能解释一下吗?

【问题讨论】:

  • 你找到"LOCAL AUTHORITY" claims的来源了吗?
  • 不。仍然为此苦苦挣扎,最终针对该成员创建了我自己的属性来保存其中的一些数据

标签: asp.net facebook asp.net-web-api oauth-2.0 claims-based-identity


【解决方案1】:

LOCAL_AUTHORITY 是 Issuer 的默认值,如果在创建声明时未指定它。例如: var claim = new Claim("LastName", "Timberlake","string", "http:/contoso.com/someissuername"); 上例中的最后一个参数是发行者。

【讨论】:

    【解决方案2】:

    我在重命名身份 cookie 时遇到了同样的问题。所以我在 2 个 cookie 中有 2 个不同的用户。删除后,旧的一期就没有了。

    【讨论】:

      【解决方案3】:

      在向您的身份添加声明时:

      // Get the claims identity
          ClaimsIdentity claimsIdentity =
              await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
      
          if (claimsIdentity != null)
          {
              // Retrieve the existing claims
              var currentClaims = await UserManager.GetClaimsAsync(user.Id);
      
              // Get the list of access token related claims from the identity
              var tokenClaims = claimsIdentity.Claims
                  .Where(c => c.Type.StartsWith("urn:tokens:"));
      
              // Save the access token related claims
              foreach (var tokenClaim in tokenClaims)
              {
                  if (!currentClaims.Contains(tokenClaim))
                  {
                      await UserManager.AddClaimAsync(user.Id, tokenClaim);
                  }
              }
          }
      

      要将这些声明保存到数据库中,您必须为用户调用 SignIn:

      // Sign in and redirect the user
          await SignInAsync(user, isPersistent: false);
      

      要稍后检索声明,您只需使用:

      var claimsIdentity = HttpContext.User.Identity as ClaimsIdentity;
      if (claimsIdentity != null)
         var claims = claimsIdentity.Claims;
      

      此代码由本文中的 sn-ps 组成:http://www.jerriepelser.com/blog/get-the-twitter-profile-image-using-the-asp-net-identity

      如果您想查看完整示例,我建议您通读一遍。我自己使用了本文中的代码,它在我的项目中对 Twitter 和 Facebook 的外部声明都很有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        • 1970-01-01
        • 2010-11-24
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 2017-10-28
        相关资源
        最近更新 更多