【问题标题】:C# WCF -- Custom Error on Authorization Fail via a custom AuthorizationManagerC# WCF -- 通过自定义 AuthorizationManager 自定义错误授权失败
【发布时间】:2011-09-18 21:38:12
【问题描述】:

我目前正在实施自定义 WCF OpenAuth AuthenticationManager,并且我已经成功地做到了。但是,我现在的问题是用户无法进行身份验证时的响应页面。我希望用户接收带有 401(未经授权)响应状态代码的 JSON 格式的消息。但是,我得到一个通用的 400 错误响应:

HTTP/1.1 400 Bad Request
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 20 Jun 2011 17:29:31 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 1765
Cache-Control: private
Content-Type: text/html
Connection: Close

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Request Error</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="http://localhost:2947/user/help">service help page</a> for constructing valid requests to the service.</p>
    </div>
  </body>
</html>

我真的很想得到类似以下的东西:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 20 Jun 2011 15:35:12 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 265
Cache-Control: private
Content-Type: application/json; charset=utf-8
Connection: Close

{"result":{"error":"Unable to authenticate using provided OAuth credentials"}}

我正在通过扩展 ServiceAuthorizationManager 并创建一个注入我的身份验证管理器的自定义 WebServiceHostFactory 来实现我的身份验证服务。

非常感谢任何帮助!

【问题讨论】:

  • 您是否考虑过使用 http 模块来拦截请求并进行 OAuth 验证?如果这样做,当验证失败时,您可以自定义响应格式。
  • @Andrew Church -- 我没有对此进行研究,但我已经读到在 AuthorizationManager 中进行站点范围的授权是最佳实践。但是,我已经能够使用调用来更改响应状态代码 `WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized;`
  • 对,只是不确定如果您实现缓存规则(AspNet 或 IIS)是否会调用 AuthorizationManager,但我很确定模块会。

标签: c# .net wcf rest restful-authentication


【解决方案1】:

所以我自己解决了这个问题。我按照MSDN 的指南进行操作。它相当直接,并指导您完成将 IErrorHandler 扩展服务添加到 WCF 服务的必要步骤。我还参考了another stack overflow post,了解如何专门以 JSON 格式返回我的错误。如果有人对此有任何疑问,我很乐意提供帮助,因为这是另一个痛苦的提醒,您可能有太多抽象。 :p

【讨论】:

  • 在返回 401 的 http 状态代码时,您是否能够使用自定义 json 对象来实现这一点?它对我来说似乎适用于所有其他状态代码,但使用 401 它将我的消息响应正文更改为不同的对象类型。
  • 我希望我能记住。到现在已经 5 年多一点了,其中大约 4 年我没有使用过 .NET。
猜你喜欢
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
  • 2016-03-19
  • 2013-03-17
  • 2019-10-01
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
相关资源
最近更新 更多