【问题标题】:How to add roles to the asp identity bearer token如何将角色添加到 asp 身份承载令牌
【发布时间】:2015-04-06 15:19:03
【问题描述】:

我实现了 OWIN 不记名令牌授权,并基于这篇文章:http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/,现在我想将角色添加到不记名令牌,以便我可以像使用用户名一样在控制器上检索它。 .identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName)); 并且能够通过User.Identity.Name 获取当前用户的用户名

【问题讨论】:

    标签: asp.net asp.net-web-api access-token user-roles bearer-token


    【解决方案1】:

    查看我在 bitoftech 上的最新帖子,我在生成令牌后阅读角色,或者您可以使用 ClaimsType.Role 手动分配角色,就像分配用户名一样。 希望这能回答您的问题。

    【讨论】:

    • 感谢Taiseer,我想在用户注册新帐户时手动添加角色...会有一个复选框供用户选择角色...
    【解决方案2】:

    http://forums.asp.net/t/1998896.aspx?Cannot+assign+claims+identity+to+a+variable+using+asp+net+WebAPI+with+Oauth+

    http://nareshjois.com/custom-information-in-asp-net-identity-cookie-ticket/

    我设法通过在名为 RoleName 的 asp 标识 AspNetUsers 表中创建一个新列来手动添加和读取新角色,并且我直接添加了角色...

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
            {
    
                context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
    
                using (AuthRepo _repo = new AuthRepo())
                {
                    UserProfile user = await _repo.FindUser(context.UserName, context.Password);
    
                    if (user == null)
                    {
                        context.SetError("invalid_grant", "The user name or password is incorrect.");
                        return;
                    }
    
                    /*var claims = new List<Claim>
                    {
                        new Claim(ClaimTypes.GivenName, user.FirstName),
                    };*/
    
                    var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                    identity.AddClaim(new Claim("sub", context.UserName));
                    identity.AddClaim(new Claim("role", "user"));
                    identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
                    identity.AddClaim(new Claim("RoleName", user.RoleName));
    
                    context.Validated(identity);
                }
            }
    

    然后我可以像这样读取与每个用户关联的角色

    var cp = User as ClaimsPrincipal;   //var cp = (ClaimsPrincipal)User;
    var roleName = ((Claim)cp.Claims.SingleOrDefault(x => x.Type == "RoleName")).Value.ToString();
    

    我个人认为这更容易维护......

    【讨论】:

      【解决方案3】:

      您可以添加一个函数来执行此操作。

       public static AuthenticationProperties CreateProperties(string userName, string Roles)
      {
          IDictionary<string, string> data = new Dictionary<string, string>
          {
              { "userName", userName },
              {"roles",Roles}
          };
          return new AuthenticationProperties(data);
      }
      

      【讨论】:

      • 这会添加到令牌响应负载,而不是令牌本身。所以角色/声明可能会被客户篡改。
      猜你喜欢
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多