处理中间件身份验证重定向逻辑是一种好方法吗?

问题描述 投票:0回答:1

我想问一下这种在中间件级别处理重定向的方法是否正常,或者如何“以良好的方式使其工作”,因为尽管它在例如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);
}
c# asp.net-core asp.net-core-mvc
1个回答
0
投票

IMO,您应该使用类似IAuthorizationRequirement的东西来处理授权内容,然后生成401异常并在其他端点上捕获一个异常或创建自定义异常中间件。

© www.soinside.com 2019 - 2024. All rights reserved.