我有一个简单的中间件类:
public class RouterMiddleware {
private readonly RequestDelegate _next;
public RouterMiddleware(RequestDelegate next)
{
this._next = next;
}
public async Task Invoke(HttpContext context)
{
if (!context.User.IsInRole("Guest"))
{
await _next.Invoke(context);
return;
}
if (context.Request.Path.StartsWithSegments("/home/")
|| context.Request.Path.StartsWithSegments("/api/profile/")
{
await _next.Invoke(context);
}
else
{
context.Response.Redirect("/home/");
return;
}
}
}
并context.User.IsInRole("Guest")在任何情况下为任何角色返回 false。有什么方法可以检查中间件类中的用户角色吗?
互换的青春
相关分类