简体   繁体   English

如何修改“此请求的授权已被拒绝。”使用过滤器HostAuthenticationFilter

[英]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.

相关问题 此请求的授权已被拒绝。 邮差 - Authorization has been denied for this request. Postman 此请求已被拒绝授权。 总是 - Authorization has been denied for this request. Always 始终获得“此请求已被拒绝授权。”消息 - Always getting the “Authorization has been denied for this request.” message “消息”:“此请求已拒绝授权。”OWIN中间件 - “Message”: “Authorization has been denied for this request.” OWIN middleware 始终使用身份2中的不记名令牌获得“对此请求的授权已被拒绝”。 - Always get “Authorization has been denied for this request.” using Bearer tokens in Identity 2? Azure AD 带有用于 Web API 的不记名令牌身份验证的 AD 无法正常工作,抛出错误,因为“此请求的授权已被拒绝”。 - Azure AD with Bearer token authentication for Web API not working throwing error as “Authorization has been denied for this request.” 发送承载令牌时,API端点返回“此请求已拒绝授权。” - API end point returning “Authorization has been denied for this request.” when sending bearer token 此请求的C#授权被拒绝 - C# Authorization has been denied for this request OAuth令牌授权(请求已被拒绝) - OAuth token authorization (request has been denied) httpclient令牌对此请求的授权已被拒绝 - httpclient token Authorization has been denied for this request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM