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

我正在尝试编写自己的授权属性,在那里我使用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();
        }
    }

我遇到的问题是我从网络API而不是预期的401获得404响应.我做错了什么?

这是asp.net核心1.

提前致谢!

解决方法

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

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...