Here是如何创建基于声明的授权属性。但是我对此工作方式有些怀疑。
给定我启动类的代码:
public void Configuration(IAppBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = ConfigurationManager.AppSettings["Authentication:Authority"],
RequiredScopes = ConfigurationManager.AppSettings["Authentication:Scopes"].Split(' ').ToList(),
PreserveAccessToken = true
});
}
我期望如果我的控制器具有此属性,并且发送了无效的令牌(无效的签名),则该请求将被自动拒绝为未授权,但该属性中的代码将被执行。
OWIN
是否不应该首先验证令牌?仅在验证声明之后,如何确保令牌有效(有效的约束,签名,未过期等)?
最佳答案
问题出在ClaimAuthorizationAttribute
中的链接问题之内-从未调用base.IsAuthorized()
,因此绕过了AuthorizeAttribute
提供的内置保护机制。
在查看索赔是否存在之后,不只是返回此处:
return token.Claims.Any(c => c.Type.Equals(this.Claim) && c.Value.Equals("True", StringComparison.OrdinalIgnoreCase));
您应该继续确保满足基类,并且令牌本身也是有效的:
var claimValid = token.Claims.Any(c => c.Type.Equals(this.Claim) && c.Value.Equals("True", StringComparison.OrdinalIgnoreCase));
if (claimValid)
return base.IsAuthorized();
else
return false;
关于c# - 基于声明的有效JWT保护安全的Web api,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42363034/