Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return 401 from WCF service

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

like image 313
Nevin Mathai Avatar asked Dec 28 '09 07:12

Nevin Mathai


2 Answers

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
like image 91
Jochen Avatar answered Sep 30 '22 21:09

Jochen


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.

like image 31
2 revs, 2 users 50% Avatar answered Sep 30 '22 20:09

2 revs, 2 users 50%