IErrorHandler returning wrong message body when HTTP status code is 401 Unauthorized

后端 未结 3 1531
余生分开走
余生分开走 2020-12-11 17:16

I have implemented IErrorHandler to handle authorization exceptions thrown within the constructor of my restful WCF service. When a general exception is caught my custom typ

3条回答
  •  死守一世寂寞
    2020-12-11 17:26

    After struggling with this for almost a full day I discovered that this was caused by an IIS setting.

    Under my API project in IIS, under the Authentication menu I had 'Forms Authentication' set to 'Enabled'. I turned off this 'feature' and the code above started working as expected. I found that this was due to another developer on my team putting code within the web.config file that altered the settings in IIS. Specifically:

    
    
        ...
        
            
        
        ...
    
    

    Further, I was able to get the Content-Type header to appear correctly by using the ContentType Property on the WebOperationContext OutgoingResponse object.

    // Get the outgoing response portion of the current context
    var response = WebOperationContext.Current.OutgoingResponse;
    
    // Add ContentType header that specifies we are using JSON
    response.ContentType = new MediaTypeHeaderValue("application/json").ToString();
    

提交回复
热议问题