我想问一下这种在中间件级别处理重定向的方法是否正常,或者如何“以良好的方式使其工作”,因为尽管它在例如chrome / firefox / postman中工作,但我仍然是不确定。
我的中间件调用:
public async Task Invoke(HttpContext context)
{
if (endpointRequireAuthentication(context.Request.Path))
{
if (!context.User.Identity.IsAuthenticated)
{
context.Request.Method = "GET";
context.Request.Path = "/Unauthorized";
}
}
await _next(context);
}
IMO,您应该使用类似IAuthorizationRequirement
的东西来处理授权内容,然后生成401异常并在其他端点上捕获一个异常或创建自定义异常中间件。