Web API2 identity2承载令牌权限更改

时间:2017-07-14 12:34:14

标签: c# oauth-2.0 asp.net-web-api2 asp.net-identity-2

使用Owin + Oauth2 + Identity2。

我有一个带有默认基本身份验证设置的网络Api,我已修改过。

我的startup.cs分部课

public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);//TODO: prob wont need this

        // Configure the application for OAuth based flow
        PublicClientId = "self";

        OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider(PublicClientId),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),//TODO: prob wont need this
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            // In production mode set AllowInsecureHttp = false
            AllowInsecureHttp = true //TODO: set debug mode
        };

        // Token Generation
        app.UseOAuthBearerTokens(OAuthOptions);
    }

我的startup.cs类部分在根

public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        ConfigureAuth(app);

        WebApiConfig.Register(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);
    }

我的applicationOAuthProvider.cs

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        //get user
        var service = new CarrierApi.CarrierManagementClient();
        var result = service.LoginAsync(context.UserName, context.Password);
        var user = result.Result.Identity;

        //TODO: log stuff here? i.e lastlogged etc?

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

        ClaimsIdentity oAuthIdentity = user;
        ClaimsIdentity cookiesIdentity = user;

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

正如您所看到的,我实际上通过对现有数据库的wcf调用来获取身份。当使用postman时,我得到/ token url并获得我的bearer token,在下一个请求中我将它传递到头部并调用我的控制器方法。

[Authorize(Roles = "Templates_Access")]
    public string Post([FromBody]string value)
    {
        return "woo";
    }

如果用户拥有不允许访问的权限,那么这非常有效。如果他们这样做的话。

但是,如果我去我们的网站使用相同的wcf和DB并更改用户权限,如果我在邮递员上发送相同的请求,它仍然允许访问,即使我已经删除了该用户分配的角色的权限。

如何确保在每次请求时“刷新”或再次检查权限?

1 个答案:

答案 0 :(得分:3)

登录用户的每个角色在登录时作为声明存储在承载令牌中,在GrantResourceOwnerCredentials方法中。如果必须授权请求,则通过默认的AuthorizationFilter实现在存储在承载令牌中的列表上搜索角色;因此,如果您更改用户的权限,则需要重新登录。

这种行为尊重Restfull体系结构的无状态约束,正如菲尔丁在他的dissertation中写的那样,这也是性能和安全性之间的良好平衡

如果您需要不同的行为,则有多种可能性。

刷新令牌

您可以使用Refresh Token,实现applicationOAuthProvider类的GrantRefreshToken方法;您可以检索刷新用户的权限并创建新的访问令牌;这是一篇值得学习的好文章how

请记住:

  • 客户端更复杂
  • 没有实时效果;你必须等待访问令牌到期
  • 如果访问令牌的生命周期很短,则必须经常更新(即使用户权限未更改),否则长寿命无法解决问题

检查每个请求的权限

您可以实现自定义AuthorizationFilter并在数据库中检查用户的权限,但这是一个缓慢的解决方案。

缓存和登录会话

您可以为GrantResourceOwnerCredentials方法中的每个登录生成用户会话的密钥(如guid),并将其作为声明存储在承载令牌中。您还必须使用两个索引将其存储在缓存系统(如Redis)中:用户会话的密钥和userId。 Redis的官方文档解释了how

当用户的渗透率发生变化时,您可以使缓存系统中该用户的每个会话无效,按userId进行搜索

如果会话有效,您可以实现自定义AuthorizationFilter并检查缓存中的每个请求,按用户会话的密钥进行搜索。

小心:这会违反无状态约束,您的架构将无法恢复

在这里,您可以找到AuthorizaAttribute filter的标准实现。 您可以创建扩展AuthorizeAttribute的自定义过滤器并覆盖IsAuthorized方法。

很可能还有其他方法,但是多久更改一次用户的权限?在许多系统中,同样在安全性是第一要求的系统中,如果在活动会话期间更改了用户的权限配置,则需要新的登录来激活新的登录。 您确定需要修改此标准行为吗?

如果您愿意,我建议使用缓存系统解决方案。

相关问题