gpt4 book ai didi

asp.net - 当我尝试强制 401 时,自定义授权过滤器总是返回 404

转载 作者:行者123 更新时间:2023-12-02 13:09:28 24 4
gpt4 key购买 nike

我正在尝试编写自己的授权属性,在其中使用 CustomAuthorization 属性对任何 Web api 方法运行一些自定义检查。

我的代码如下:

   [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class CustomAuthorization : AuthorizationFilterAttribute
{
public override void OnAuthorization(AuthorizationContext context)
{
//// Attempt 1 - 404 error.
//// Doesnt block method with this attribute from executing (not desired behaviour).

//context.HttpContext.Response.StatusCode = 401;
//return;

//// Attempt 2 - 404 result.
//// Code with attribute doesnt execute (desired).
//// Error thrown says: An exception of type 'System.Web.Http.HttpResponseException' occurred in <namespace> but was not handled in user code
//// Additional information: Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.

//throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized));

// Attempt 3 - 404 result.
// Code with attribute doesnt execute (desired).
context.Result = new HttpUnauthorizedResult();
}
}

我遇到的问题是我从 Web api 收到 404 响应,而不是预期的 401。我做错了什么?

这是 asp.net core 1。

提前致谢!

最佳答案

这可能是因为您有身份验证设置来重定向到 401 响应的登录页面,但找不到该登录页面(发生在我身上)。

关于asp.net - 当我尝试强制 401 时,自定义授权过滤器总是返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36895289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com