2

I am currenlty working on implementing a custom WCF OpenAuth AuthenticationManager and I have successfully done so. My problem now however, is the response pages when the user fails to authenticate. I would like for the user to receive a JSON-formatted message with a 401 (Unauthorized) response-status code. However, I get a generic, 400-error response as such:

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>

I'd really like to get something more like the following:

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"}}

I am implementing my auth-service by extending the ServiceAuthorizationManager and creating a custom WebServiceHostFactory that injects my auth-manager.

Any help is greatly appreciated!

  • Have you thought about using an http module to intercept the request and do your OAuth validation? If done this way, when the validation fails you could customize the response format. – Andrew Church Jun 21 '11 at 14:33
  • @Andrew Church -- I have not looked into this, but I have read that it is best practice to do your site-wide authorization in the AuthorizationManager. I have however been able to change the response status code using the call ` WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized;` –  Jun 21 '11 at 15:39
  • Right, just not sure if AuthorizationManager would even get called were you to implement cache rules (AspNet or IIS), but I'm pretty sure a module would. – Andrew Church Sep 14 '11 at 15:21

1 Answers1

3

So I figured this out on my own. I followed the guide at MSDN. It is fairly straight forward and guides you through the necessary steps to adding the IErrorHandler extension-service to you WCF service. I also referenced another stack overflow post for how to return my errors specifically in a JSON format. If anyone has any questions on this, I'll be glad to help because this was another painful reminder that you CAN have too much abstraction. :p

Community
  • 1
  • 1
  • Were you able to get this working with a custom json object when returning a http status code of 401? It seems to work correctly for me with all other status codes but with 401 it changes my message response body to a different object type. – John Meyer Jul 06 '16 at 17:27
  • I wish I could remember. Having been a little over 5 years now, I haven't used .NET in about 4 of them. –  Jul 06 '16 at 20:34