【问题标题】:How to re-validate token for multi-tenant ASP.NET Identity?如何重新验证多租户 ASP.NET 身份的令牌?
【发布时间】:2016-12-17 16:23:39
【问题描述】:

我已经实现了一个自定义OAuthAuthorizationServerProvider 来为帐户登录添加域约束。一切都很好。但是,我遇到了一个问题,一旦用户获得令牌,他们就可以将它用于他们想要的任何系统。例如:

他们使用正确的用户名和密码请求TokenEndpointPath(假设它是租户 1 的管理员帐户):http://localhost:40721/api/v1/account/auth 并接收承载令牌。

现在他们使用它来访问:http://localhost:40720/api/v1/info/admin,属于租户 0。该请求被视为已授权。

我尝试更改 CreateProperties 方法,但没有帮助:

    public static AuthenticationProperties CreateProperties(string userName)
    {
        var tenant = DependencyUtils.Resolve<IdentityTenant>();
        IDictionary<string, string> data = new Dictionary<string, string>
        {
            { "userName", userName },
            { "tenantId", tenant.Tenant.Id.ToString() },
        };
        return new AuthenticationProperties(data);
    }

我也尝试过覆盖ValidateAuthorizeRequest,但在我的调试中从未调用过它。

我是否需要在其他任何地方进行检查,因此令牌仅对域/正确的租户有效?

(注意:一个租户可能有多个域,所以如果我可以手动对正确的租户执行帐户检查而不是坚持一个域,那就太好了。但是,如果我能做到这一点,那就太好了,否则,只需限制域的令牌是可以的)

【问题讨论】:

    标签: asp.net asp.net-web-api2 multi-tenant asp.net-identity-2


    【解决方案1】:

    不是对我的问题的直接回答(因为它不在 ASP.NET Identity 工作流中),但我应用的最简单的解决方法是改用 ActionFilterAttribute

    public class DomainValidationFilter : ActionFilterAttribute
    {
    
        public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            // Other Code...
    
            // Validate if the logged in user is from correct tenant
            var principal = actionContext.ControllerContext.RequestContext.Principal;
            if (principal != null && principal.Identity != null && principal.Identity.IsAuthenticated)
            {
                var userId = int.Parse(principal.Identity.GetUserId());
                // Validate against the tenant Id of your own storage, and use this code to invalidate the request if it is trying to exploit:
               actionContext.Response = actionContext.Request.CreateResponse(System.Net.HttpStatusCode.Unauthorized, "Invalid Token"); 
    
            }
    
            return base.OnActionExecutingAsync(actionContext, cancellationToken);
        }
    
    }
    

    然后通过在FilterConfigWebApiConfig 中注册来将过滤器应用于所有操作:

    config.Filters.Add(new DomainValidationFilter());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      相关资源
      最近更新 更多