asp.net – 当我试图强制401时,自定义授权过滤器总是返回404
发布时间:2020-07-31 13:22:09  所属栏目:asp.Net  来源:互联网 
            导读:我正在尝试编写自己的授权属性,在那里我使用CustomAuthorization属性对任何web api方法执行一些自定义检查. 我的代码如下: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class C
                
                
                
            | 
                         我正在尝试编写自己的授权属性,在那里我使用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响应的登录页面,并且找不到登录页面(发生在我身上).(编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
相关内容
- asp.net – MVC 4导出到CSV – 另存为对话框在Chrome和Fire
 - asp.net – 当我试图强制401时,自定义授权过滤器总是返回40
 - asp.net – NHibernate – ManagedWebSessionContext和WebS
 - 我可以使用ASP.NET成员身份实体框架吗?
 - asp.net – 在调用异步方法时不使用等待来防止死锁
 - asp.net-mvc – 我如何可以渲染局部视图在asp.net mvc 3
 - ASP.NET通过自定义函数实现对字符串的大小写切换功能
 - ASP.NET页面验证
 - asp.net – IIS 404自定义错误不能按预期工作
 - asp.net-mvc-3 – MVC3剃须刀:是否可以渲染传统的ASCX?
 
推荐文章
            站长推荐
            - 为什么asp.net将页面包装在一个表单中?
 - 实体框架 – 实体框架核心更新许多对许多
 - asp.net-mvc – ActionResult上的自定义属性
 - asp.net-mvc-4 – MVC 4 Razor如果拆分div标签
 - ASP.NET MVC3中的HTML反而不是JSON的IIS响应
 - 在ASP.net中使用NVP API时,PayPal SetExpressChe
 - asp.net-mvc – ASP.NET MVC中的WebApi [FromUri
 - 什么用于ASP.NET的成员资格
 - asp.net-mvc – asp.net mvc如何正确测试控制器
 - asp.net-mvc – 带有asp.net mvc 4和EntityFrame
 
热点阅读
            