Http 从WCF服务返回401
如何从WCF服务返回HTTP 401 根据您需要执行授权检查的时间,您可以在Http 从WCF服务返回401,http,wcf,http-status-code-401,Http,Wcf,Http Status Code 401,如何从WCF服务返回HTTP 401 根据您需要执行授权检查的时间,您可以在HttpModule中使用以下方法执行授权检查: HttpContext context = HttpContext.Current; context.Response.StatusCode = 401; context.Response.End(); 如果您正在编程REST服务,可以通过以下方式完成: private IWebOperationContext context = new WebOperationCon
HttpModule
中使用以下方法执行授权检查:
HttpContext context = HttpContext.Current;
context.Response.StatusCode = 401;
context.Response.End();
如果您正在编程REST服务,可以通过以下方式完成:
private IWebOperationContext context = new WebOperationContextWrapper(WebOperationContext.Current); // Get the context
context.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized; // Set the 401
如果您正在使用WCF REST初学者工具包中的WebServiceHost2工厂,还可以抛出特定的
WebProtocolException
,并指定HTTP返回代码:
(来源:)
(来源:)
(来源:) 还有一个与401状态代码相对应的
HttpStatusCode.Unauthorized
有关指定HTTP返回码的各种方法的更多详细信息,请参阅Rob Bagby出色的博客文章。(截图来自Rob的博客文章——这一切都归功于他。)
注:
MSDN:使用WCF REST端点时(WebHttpBinding和
WebHttpBehavior或WebScriptEnableBehavior)上的HTTP状态代码
相应地设置响应。但是,WebFaultException可以是
与非REST端点一起使用,其行为类似于常规端点
错误例外
不完全正确…您可以使用任何XHTPBindings执行此操作。您正在执行[OperationContract,WebInvoke][Service]操作吗?
throw new WebFaultException(System.Net.HttpStatusCode.Unauthorized);