【问题标题】:Sending sensitive information with OAuth Bearer Tokens using asp.net claims使用 asp.net 声明通过 OAuth Bearer Tokens 发送敏感信息
【发布时间】:2016-04-19 17:38:02
【问题描述】:

我正在使用 asp.net 声明将敏感信息添加到 OAuth 持有者令牌中。令牌由 wep api 生成,并由客户端为每个请求发送到 api。

这是生成令牌的函数

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            using (var applicationDb = new ApplicationDbContext())
            using (var userStore = new UserStore<AppUser>(applicationDb))
            {
                IApplicationUserService applicationUserService = new ApplicationUserService(userStore, null, null, null);
                try
                {
                    var appUser = await applicationUserService.AuthenticateUserAsync(context.UserName, context.Password);

                    if (appUser == null)
                        context.SetError("InvalidCredentials", "The email or password that you entered is incorrect.");
                    else if (!appUser.EmailConfirmed)
                        context.SetError("EmailVerification", "You must verify your email address before signing in.");
                    else
                    {

                        var roles = applicationUserService.GetUserRoles(appUser.Id);
                        var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                        identity.AddClaim(new Claim(ClaimTypes.UserId, appUser.Id));
                        identity.AddClaim(new Claim(ClaimTypes.Roles, string.Join(",", roles.ToArray())));

                        context.Validated(identity);
                    }
                }
                catch (Exception exception)
                {
                    context.SetError("server error", "Server error! Please try again later.");
                }
            }
        }

        public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            context.Validated();
        }

在上面的代码中,我将 userId 和 role 声明添加到令牌。我正在使用这些角色来授予对 Web API 中特定信息的访问权限。

这种方法有多安全?我可以信任令牌中的信息吗?用户可以篡改令牌并更改角色吗?

如果是这样,我该如何防止这种情况发生?我应该使用数据库重新验证令牌中的所有信息吗?

【问题讨论】:

    标签: asp.net-web-api oauth claims-based-identity bearer-token


    【解决方案1】:

    只要令牌已签名并且签名有效,就应该没问题。如果用户篡改了令牌,则签名将不再有效。根据令牌类型,此签名以各种方式完成。这是一篇关于该主题的精彩文章:

    http://www.cloudidentity.com/blog/2014/03/03/principles-of-token-validation/

    您可能还会发现这些主题很有用:

    Are OAuth2 bearer tokens signed?

    How to validate an OAuth 2.0 access token for a resource server?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-26
      • 2011-06-24
      • 2012-10-31
      • 2015-09-21
      • 2014-12-09
      • 2016-10-10
      • 1970-01-01
      • 2016-04-12
      相关资源
      最近更新 更多