Return 401 from WCF service

Nevin Mathai picture Nevin Mathai · Dec 28, 2009 · Viewed 9.4k times · Source

How can I return a HTTP 401 from a WCF service?

Answer

Jochen picture Jochen · Dec 28, 2009

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