【问题标题】:WebAPI Authorization Attribute not being called with IdentityServer3 Bearer Token未使用 IdentityServer3 Bearer Token 调用 WebAPI 授权属性
【发布时间】:2017-08-18 03:05:06
【问题描述】:

我有一个 WebAPI 2 项目,它使用由 IdentityServer3 令牌提供者颁发的令牌。在我的 Startup.cs 文件中,我实现了 IdentityServerBearerTokenAuthorization 中间件,它与全局 AuthorizateAttribute 过滤器一起要求请求中存在有效令牌。但是,我还添加了 ClaimsTransformation,因此我可以从使用隐式流发布的令牌或为客户端凭据流发布的令牌中的声明中提取“角色”。我不能在这里使用范围,因为我有 1 个范围可以让您访问使用我的 API,但不允许所有客户端使用所有 api 端点。

Startup.cs

 JwtSecurityTokenHandler.InboundClaimTypeMap.Clear();


        app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions()
        {
            Authority = ConfigurationManager.AppSettings["IdentityServer"],
            RequiredScopes = new[] { "my.api" },
        });



        httpConfig.MapHttpAttributeRoutes();
        httpConfig.Filters.Add(new AuthorizeAttribute());


        //SwaggerConfig.Register(httpConfig);

        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(httpConfig);
        app.UseWebApi(httpConfig);

        app.UseClaimsTransformation(identity =>
        {
            var principal = new ClaimsPrincipal(identity);
            if (!identity.HasClaim(c => c.Type == "name") && identity.HasClaim(c => c.Type == "client_name"))
            {
                identity.Identities.First().AddClaim(new Claim("name", identity.Claims.First(c => c.Type == "client_name").Value));
            }

            //we want to remove the client_ from the claims so we can evaluate clients like they are users
            if (identity.Claims.Any(c => c.Type.Contains("client_")))
            {
                foreach (var claim in identity.Claims.Where(c => c.Type.Contains("client_")))
                {
                    var newClaimType = claim.Type.Replace("client_", "");
                    identity.Identities.First().AddClaim(new Claim(newClaimType, claim.Value));
                }
            }

            //set the scopes as roles also
            if (identity.Claims.Any(c => c.Type == "scope"))
            {
                identity.Identities.First().AddClaims(identity.Claims.Where(c => c.Type == "scope").Select(c => new Claim("role", c.Value)));
            }

            return Task.FromResult(principal);
        });

在我的 APIController 操作中,我有一个 Authorize 属性,其中定义了 Roles 属性。全局 Authorize 属性正在工作,但从未发生过角色检查。我错过了什么吗? \ API 控制器

    [HttpDelete]
    [Authorize(Roles = "item.deleter")]
    [Route("{itemId:guid}")]
    public async Task<HttpResponseMessage> DeleteAsync([ValidGuid] Guid itemId)
    {
        _log.Audit.Info($"Received Delete request for item {itemId} from user {User.Identity?.Name}.");
        if (!ModelState.IsValid)
      ....

【问题讨论】:

    标签: c# asp.net-web-api identityserver3


    【解决方案1】:

    您的 authroize 属性很可能在您的声明转换触发之前触发。

    在您自己的管道中,您在声明转换之前添加了 webAPI。当您的请求沿管道传输时,Web API 将获取请求并在声明转换可以发挥作用之前对其运行授权。

    尝试将UseClaimsTransformation 移到UseWebApi 之前

    【讨论】:

    • 我尝试移动索赔转换,但我仍然遇到问题。事实上,我创建了自己的属性来查看发生了什么,但它也没有受到影响(它确实从 Authorize 继承,所以我接下来要尝试的不是从 Authorize 继承以完全退出 ASP.NET Identity 工作流) .具有全局授权属性并从方法覆盖它是否存在任何已知问题?我的理解是它将被视为 AND 类型的操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2017-04-05
    相关资源
    最近更新 更多