【发布时间】:2019-02-07 16:54:06
【问题描述】:
在一个 WebAPI .net 核心项目中,我创建了一个验证 api 密钥的中间件类。通过验证它,它会在调用方法中检索密钥具有的权限(用户或管理员)。
我通过一个开关来设置这样的原理
GenericIdentity identity = new GenericIdentity("API");
GenericPrincipal principle = null;
//we have a valid api key, so set the role permissions of the key
switch (keyValidatorRes.Role)
{
case Roles.User:
principle = new GenericPrincipal(identity, new[] { "User" });
context.User = principle;
break;
case Roles.Admin:
principle = new GenericPrincipal(identity, new[] { "Admin" });
context.User = principle;
break;
default:
principle = new GenericPrincipal(identity, new[] { "Other" });
context.User = principle;
break;
}
关于我有的控制器方法[Authorize(Roles = "Admin")]
验证经过身份验证的 api 密钥的角色
如果用户有管理原则,它会按预期进行。但是,如果它有用户或其他原则,那么我会收到关于
的错误没有 DefaultForbidScheme
我四处搜索并使用客户方案将身份验证添加到我的 startup.cs
services.AddAuthentication(options=> {
options.DefaultForbidScheme = "forbidScheme";
options.AddScheme<AuthSchemeHandle>("forbidScheme", "Handle Forbidden");
});
并创建了 AuthSchemeHandle
public class AuthSchemeHandle : IAuthenticationHandler
{
private HttpContext _context;
public Task<AuthenticateResult> AuthenticateAsync()
{
return Task.FromResult(AuthenticateResult.NoResult());
}
public Task ChallengeAsync(AuthenticationProperties properties)
{
throw new NotImplementedException();
}
public Task ForbidAsync(AuthenticationProperties properties)
{
return Task.FromResult(AuthenticateResult.Fail("Failed Auth"));
}
public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
{
_context = context;
return Task.CompletedTask;
}
}
现在,如果原则没有管理员,它会失败而没有错误,但在 API 上返回的响应是 200,没有内容。我期待 4xx 回复消息“验证失败”
我只是想弄清楚为什么它不像预期的那样,虽然它看起来“已修复”,但我不明白它是如何修复它的。
我应该这样做有更好的方法吗?
问候 标记
【问题讨论】: