【问题标题】:Customize WebAPI "Access Denied" response自定义 WebAPI“拒绝访问”响应
【发布时间】:2015-07-06 20:53:26
【问题描述】:

我正在开发一个带有 OAUTH 2.0 身份验证的 ASP.net WebAPI 应用程序,带有分离的 STS(令牌服务)和自定义 JSON 格式化程序 (ServiceStack.Text)。

我正在尝试自定义拒绝访问对象/消息以使其与其余错误消息同质化,但我还没有找到更改它的方法。

我也在考虑在这种情况下使用默认格式化程序。

例子:

{
  "Message": "Authorization has been denied for this request."
}

示例结果:

{
  "message": "... insert error message here ...",
  "details": null
}

提前致谢。

【问题讨论】:

    标签: c# asp.net rest asp.net-web-api oauth-2.0


    【解决方案1】:

    您可以使用可以为其定义其成员的类返回当前HttpActionContext 的自定义响应。

                public override void OnActionExecuting(HttpActionContext actionContext)
                {
                        bool isAuthenticated = IsAuthenticated(actionContext);
    
                        if (!isAuthenticated)
                        {
                            actionContext.Response = actionExecutedContext.Request.CreateResponse<CustomActionResult>(HttpStatusCode.Unauthorized, new CustomActionResult
                            {
                                Message = "... insert error message here ...",
                                Details = null
                            });
                        }
                    }
                }
    
                public class CustomActionResult
                {
                    public string Message { get; set; }
                    public string Details { get; set; }
                }
    

    【讨论】:

    • 感谢您的建议,但它如何在没有身份验证的情况下作用于 API?
    • 您可以在此方法中定义您自己的验证 Web api 请求的逻辑 - IsAuthenticated(),这必须特定于您的应用程序的要求。我没有发布与我的应用程序相关的逻辑 cos,可能对您没有用。您知道 API 请求何时应被拒绝为未经授权,何时应被允许。
    【解决方案2】:

    要更改整个 ASP.NET 网站的所有“拒绝访问”消息,您可以为从您的网站返回的所有未授权状态创建一个 HttpModule。在 HttpModule 中,您可以处理 EndRequest 事件,您可以检查 response.StatusCode 如果它是 401,您可以将消息更改为您想要的任何内容。像这样:

        public class AuthorizeMsgModule : IHttpModule
        {            
            public void Init(HttpApplication context)
            {                
                context.EndRequest += OnApplicationEndRequest;
            }
    
    
            // If the request was unauthorized, modify the response sent to the user
            private static void OnApplicationEndRequest(object sender, EventArgs e)
            {
                var response = HttpContext.Current.Response;
                if (response.StatusCode == 401)
                {
                    response.ClearContent();
                    response.Write("{\"message\": \"... insert error message here ...\",\"details\": null}");                    
                }
            }
    
            public void Dispose()
            {
            }
        }
    

    在您的 web.config 中注册您的模块,例如:

    <modules runAllManagedModulesForAllRequests="true">
      <add name="AuthorizeMsgModule" type="mynamespace.AuthorizeMsgModule, myassembly" />
    </modules>
    

    这将在您返回 401 状态时为您提供您编写的内容。正如您在 fiddler 中看到的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 2011-05-21
      相关资源
      最近更新 更多