asp.net – 当我试图强制401时,自定义授权过滤器总是返回404
发布时间:2020-07-31 11:21:47 所属栏目: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 – 如何禁用.NET事件日志警告?
- 如何在ASP.NET Web应用程序中打开一个SectionGroup?
- asp.net-mvc-3 – 剃刀引擎 – 如何根据不同的条件渲染不同
- ASP.NET:WebResource.axd调用404错误:如何知道哪个程序集
- asp.net-mvc – 获取ControllerName和ActionName并在Master
- asp.net-mvc – 从一个项目到另一个项目调用控制器
- 如何在ASP.NET和C#中加载下拉列表?
- asp.net-mvc – SelectListItem中的Selected属性永远不会起
- asp.net-mvc – ASP.NET MVC推荐的依赖注入框架是什么?
- asp.net-mvc-3 – outputcache mvc3只注销了用户缓存
推荐文章
站长推荐
- 如何在ASP.NET Web应用程序中打开一个SectionGro
- asp.net-mvc-3 – 将数组传递给RouteValues,并将
- asp.net-mvc – MVC 3布局页面,Razor模板和下拉列
- 修改FCKEditor实现将图片、文件等保存到数据库
- .net4.0中的ClientIDMode
- asp.net-web-api – WebApi 2.1 PUT抛出错误415
- ASP.NET AJAX中的$create函数是什么?
- asp.net-mvc-3 – Azure网站上的RavenDb – 访问
- Asp.Net 5分钟实现网页实时监控
- Asp.net 实现Session分布式储存(Redis,Mongodb,M
热点阅读