我有一个简单的中间件类:
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。有没有办法检查中间件类中的用户角色?
发布于 2018-05-18 19:35:13
问题出在Startup.cs
上。中间件的顺序是:
app.UseMiddleware<Middleware.RouterMiddleware>();
app.UseAuthentication();
但必须是:
app.UseAuthentication();
app.UseMiddleware<Middleware.RouterMiddleware>();
https://stackoverflow.com/questions/50407880
复制相似问题