目前 middleware 中的代码是这么写的
public class AuthorizeMiddleware{ private RequestDelegate _next; public AuthorizeMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context, IUserService userService) { var identity = context.User.Identity; if (identity.IsAuthenticated && await userService.IsUserInRole(ROLE_NAME, identity.Name)) { context.User.AddIdentity(new ClaimsIdentity("Basic", ClaimTypes.Role, ROLE_NAME)); } await _next(context); } }
在 Authorization Policy 中使用这个角色进行授权,但不起作用
services.AddMvc(o => { var policy = new AuthorizationPolicyBuilder() .RequireRole(ROLE_NAME) .Build(); o.Filters.Add(new AuthorizeFilter(policy)); });
请问如何解决这个问题?
守着星空守着你