Rob*_*abe 6 asp.net attributes asp.net-web-api2
我正在尝试编写自己的授权属性,在那里我使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我从网络API而不是预期的401获得404响应.我做错了什么?
这是asp.net核心1.
提前致谢!
| 归档时间: |
|
| 查看次数: |
925 次 |
| 最近记录: |