我有一个简单的中间件类: 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。有什么方法可以检查中间件类中的用户角色吗?
1 回答
![?](http://img1.sycdn.imooc.com/533e4c0500010c7602000200-100-100.jpg)
互换的青春
TA贡献1797条经验 获得超6个赞
问题出在Startup.cs
. 中间件的顺序是:
app.UseMiddleware<Middleware.RouterMiddleware>(); app.UseAuthentication();
但必须是:
app.UseAuthentication(); app.UseMiddleware<Middleware.RouterMiddleware>();
- 1 回答
- 0 关注
- 190 浏览
添加回答
举报
0/150
提交
取消