来源:https://github.com/jhonduck22/ApiExample23231232
我有一个检查JWT令牌(“管理员”角色)的授权,但是现在我想要高级角色和更多用Dapper检查数据库而不是在访问令牌中保存角色,所以我创建了一个自定义UserStore 。问题是当我调用控制器时,控制器返回Not Found而不是检查角色。
我有下一个控制器,在没有[授权]的情况下返回“200 Ok”,在[授权]中返回“404 Not Found”
[HttpGet]
[Authorize(Roles = "TestingIdentity")]
public IActionResult IdentityTest()
{
return Ok("Responsee");
}
UserStore
public class UserStore<IdentityUser> :
IUserStore<IdentityUser>
{
// ...
}
UserRoleStore
public class UserRoleStore<IdentityUser> :
IUserRoleStore<IdentityUser>
{
// ...
public Task<bool> IsInRoleAsync(TUser user, string roleName, CancellationToken cancellationToken)
{
return Task.FromResult(true);
}
IdentityUser
public class IdentityUser
{
public Guid Id { get; set; }
public string Mail { get; set; }
}
启动
services.AddIdentity<IdentityUser, IdentityRole>()
.AddUserStore<UserStore>()
.AddRoleStore<UserRoleStore>();
services.AddAuthorization(auth =>
{
auth
.AddPolicy("AdministratorOnly", new AuthorizationPolicyBuilder()
.RequireRole("Administrator")
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser()
.Build());
auth
.AddPolicy("TestingIdentityOnly", policy => policy.RequireRole("TestingIdentity")
.RequireAuthenticatedUser());
});