asp.net – 当我试图强制401时,自定义授权过滤器总是返回404
发布时间:2020-12-15 21:06:01 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试编写自己的授权属性,在那里我使用CustomAuthorization属性对任何web api方法执行一些自定义检查. 我的代码如下: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,AllowMultiple = false,Inherited = false)] public class Cus
我正在尝试编写自己的授权属性,在那里我使用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中找到10月的最后一个星期日c#
- 问题在ASP.NET 5(Core)中添加对类库项目的引用
- asp.net – System.Web.Cache,会话级别或应用程序级别
- asp.net – FormsAuthentication.SetAuthCookie在IE10和IIS
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-
- asp.net – 将MasterPage ImageButton事件传递给内容页面
- asp.net – SignalR:如何强制认证/终止中心连接服务器端
- asp.net – 具有管道模式=集成的IIS 7.0不会在ASP .NET中加
- asp.net – 如何删除特定页面中的cookie?
- asp.net-mvc – ASP.NET MVC实体框架关系绑定
推荐文章
站长推荐
- asp.net – ASP会员奇数问题
- asp.net – 是MemoryCache范围会话还是应用程序?
- asp.net-mvc – 在NerdDinner中首次尝试Linq to
- asp.net – 手动将MVC Web应用程序从3升级到4,无
- asp.net – HttpError不会显示自定义错误页面
- asp.net-mvc-3 – 与BCrypt.net合作
- asp.net – Datagrid:计算页脚中列的平均值或总
- asp.net – Android C2DM获取(401)未经授权
- asp.net-mvc-3 – Razor reseverd words
- asp.net – 反伪造cookie令牌和表单字段令牌在MV
热点阅读