我有一个自定义授权属性。如何从自定义授权属性返回401代码?
当这在WebAPI中实现时,OnActionExecuting
方法通过了一个HttpActionContext
对象,我可以在其中更新响应,例如,
actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
但是,在MVC实现时传递的对象是ActionExecutingContext
对象,其中包含的回应,但它是只读的,这意味着我不能以同样的方式返回授权拒绝。
,因为它是只读这是行不通的:
context.HttpContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
这也将无法正常工作,因为它是只读:
HttpContext.Current.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
我怎样才能在MVC可写的反应,我可以这样设置?
这是一个很好的资源:https://dusted.codes/demystifying-aspnet-mvc-5-error-pages-and-error-logging –
听起来你正在做定制ActionFilter代替AuthorizationFilter。 –