我目前正在努力实现一个自定义的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来实现身份验证服务。
任何帮助都是非常感谢的!
发布于 2011-06-24 22:49:29
所以我自己解决了这个问题。我跟着MSDN的导游走。它相当简单,并指导您完成向您的IErrorHandler服务添加WCF扩展服务的必要步骤。我还引用了another stack overflow post来了解如何以JSON格式返回错误。如果任何人对此有任何问题,我很乐意提供帮助,因为这是另一个痛苦的提醒,你可以有太多的抽象。:p
https://stackoverflow.com/questions/6415193
复制相似问题