【问题标题】:Bearer Token Authentication and Password Change承载令牌认证和密码更改
【发布时间】:2014-12-18 22:36:28
【问题描述】:

现在我正处于 Web API 中 owin 持有者令牌身份验证的学习阶段。该代码是使用基于令牌和 cookie 的身份验证实现的。代码是

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        try
        {

              using (UserManager<ApplicationUser> userManager = userManagerFactory())
                {

                    ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);


                    if (user == null || user.IsDeleted)
                    {
                        context.SetError("invalid_grant", "The user name or password is incorrect.");
                        return;
                    }

                    ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,
                        context.Options.AuthenticationType);
                    ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
                        CookieAuthenticationDefaults.AuthenticationType);

                    var roleName = await GetRoleName(user.Roles.First().RoleId);

                    AuthenticationProperties properties = CreateProperties(user.UserName, roleName);
                    AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
                    context.Validated(ticket);
                    context.Request.Context.Authentication.SignIn(cookiesIdentity);
                }

        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

        public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            // Resource owner password credentials does not provide a client ID.
            if (context.ClientId == null)
            {
                context.Validated();
            }

            return Task.FromResult<object>(null);
        }

        public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context)
        {
            if (context.ClientId == _publicClientId)
            {
                Uri expectedRootUri = new Uri(context.Request.Uri, "/");

                if (expectedRootUri.AbsoluteUri == context.RedirectUri)
                {
                    context.Validated();
                }
            }

            return Task.FromResult<object>(null);
        }

代码是同事实现的,有点疑惑。

  1. 令牌身份验证基于生成的令牌。我为我的用户生成了一个令牌,其角色是“管理员”。现在我可以访问受限操作,因为用户具有“管理员”角色。但在那之后,我将同一老用户的角色更改为“用户”。现在使用相同的旧令牌,即使他现在不在“管理员”中,我也可以访问该资源。实际上我读了一些文章,这是用额外的自定义逻辑实现的。没关系

  2. 现在我将用户密码更改为其他密码。现在本身,我可以使用相同的旧令牌访问资源。我认为这也不好,即使我也创建了短暂的令牌。

请指导逮捕这个或我错过了什么?当我调用带有“授权”标头的操作时,实际调用的是哪个方法

【问题讨论】:

    标签: oauth-2.0 owin asp.net-web-api2 bearer-token


    【解决方案1】:

    嗯,没有“直接”的方式来撤销授予的访问令牌或“注销”。如果用户拥有令牌,那么他可以访问受保护的服务器资源,直到令牌过期。间接方法是将授予用户的每个令牌的 token_id 存储在数据库中,并对每次调用进行数据库检查,这是我不推荐的。

    因此在某些情况下,最好将刷新令牌与访问令牌一起使用。因此,您发出短暂的访问令牌 (15) 分钟,然后使用刷新令牌来获取新的访问令牌。这里的好处是刷新令牌可以从后端系统撤消,因此可以控制它们。

    查看我的帖子,了解如何启用 OAuth refresh tokens in ASP.NET Web API

    【讨论】:

    • 您好,谢谢。我已经阅读了您所有与此相关的文章,并且只有我通过刷新令牌才可以做到这一点。我只是在这里询问任何替代选项,因为即使刷新令牌是 15 分钟,也无法处理 15 分钟内的任何更改。只是为了清理
    猜你喜欢
    • 2014-10-11
    • 2019-06-27
    • 2021-08-28
    • 2020-04-07
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    相关资源
    最近更新 更多