[英]How to Modify “Authorization has been denied for this request.” Using filter HostAuthenticationFilter
Using Bearer token authentication. 使用承载令牌认证。 If response is failure then, need to return additional field along with below message: 如果响应失败,则需要返回附加字段以及以下消息:
401 UnAuthorize response
{Message: "Authorization has been denied for this request"}
How to include additional field in 401 response message. 如何在401响应消息中包括其他字段。 It will be like as below: (include additional field 'ID' that indicate failure tracking ID). 它将如下所示:(包括指示故障跟踪ID的附加字段“ ID”)。
{Message: "Authorization has been denied for this request",
Id: 1}
filter.config is below: filter.config如下:
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
You need to provide your own implementation the authorization attributes by overriding AuthorizeAttribute. 您需要通过覆盖AuthorizeAttribute为自己的实现提供授权属性。
public class YourCustomAuthorization : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
{
actionContext.Response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.Unauthorized,
Content = new StringContent("You Message")
};
}
}
and use it as 并用作
[CustomAuthorization]
public IHttpActionResult Get()
{
return Ok();
}
Also check this: http://prideparrot.com/blog/archive/2012/6/customizing_authorize_attribute 还要检查以下内容: http : //prideparrot.com/blog/archive/2012/6/customizing_authorize_attribute
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.