How can I return a HTTP 401 from a WCF service?
If you are programming a REST-service it can be done this way:
private IWebOperationContext context = new WebOperationContextWrapper(WebOperationContext.Current); // Get the context
context.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized; // Set the 401
throw new WebFaultException(System.Net.HttpStatusCode.Unauthorized);
Notes:
MSDN: When using a WCF REST endpoint (WebHttpBinding and WebHttpBehavior or WebScriptEnablingBehavior) the HTTP status code on the response is set accordingly. However, WebFaultException can be used with non-REST endpoints and behaves like a regular FaultException.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With