如何從WCF服務返回HTTP 401?從WCF服務返回401
回答
取決於當你需要做的授權檢查,你可以使用類似做在一個HttpModule
如下:
HttpContext context = HttpContext.Current;
context.Response.StatusCode = 401;
context.Response.End();
不完全如此......您可以使用任何xHttpBindings來做到這一點。 – WayneC 2009-12-28 16:24:55
如果您正在編寫一個REST的服務是可以做到這樣:
private IWebOperationContext context = new WebOperationContextWrapper(WebOperationContext.Current); // Get the context
context.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized; // Set the 401
如果您使用從WCF REST入門工具包WebServiceHost2工廠,你也可以拋出具體WebProtocolException
並指定一個HTTP返回代碼:
alt text http://www.robbagby.com/wp-content/uploads/effective-error-handling-with-wcf-rest/image_thumb_5.png alt text http://www.robbagby.com/wp-content/uploads/effective-error-handling-with-wcf-rest/image_thumb_10.png alt text http://www.robbagby.com/wp-content/uploads/effective-error-handling-with-wcf-rest/image_thumb_12.png
還有一個HttpStatusCode.Unauthorized
對應於401個狀態碼。
有關指定HTTP返回碼的各種方式的更多詳細信息,請參閱Rob Bagby的優秀博客文章Effective Error Handling with WCF REST。 (截圖是來自Rob的博客文章 - 他是值得所有的功勞這一點。)
throw new WebFaultException(System.Net.HttpStatusCode.Unauthorized);
注: 「MSDN:當使用WCF REST端點(的WebHttpBinding和WebHttpBehavior或WebScriptEnablingBehavior)在響應的HTTP狀態代碼但是,WebFaultException可以與非REST端點一起使用,並且像普通的FaultException一樣運行。「
- 1. 從WCF服務返回MembershipUser
- 2. 從wcf服務返回xdocument
- 3. 返回XML從WCF服務
- 4. 從wcf服務返回wcf/json結果
- 5. WCF有時會返回401
- 6. 從WCF服務返回NHibernate映射類
- 7. 如何從WCF Web服務返回XML?
- 8. 從WCF數據服務返回BSonDocuments
- 9. 如何從WCF服務返回Json?
- 10. 從WCF服務返回原始XML
- 11. 從WCF服務中返回SqlDataReader
- 12. 從WCF服務返回接口
- 13. 從WCF服務返回的System.EngineExecutionException
- 14. 從WCF服務返回數據表
- 15. WCF Ajax服務返回值
- 16. WCF Rest服務返回HttpWebResponse
- 17. Restful WCF服務 - 返回byte []?
- 18. WCF服務返回Response.Write
- 19. WCF服務返回iCal
- 20. Azure服務總線AddMessage返回401
- 21. 返回其他服務的WCF服務
- 22. 從WCF服務返回大量數據到ASP.NET Web服務
- 23. 從服務布料演員/服務中返回wcf流
- 24. WCF REST服務如何返回401 Unauthorize代碼,但也是一個對象?
- 25. Wcf數據服務 - 401錯誤
- 26. WCF REST服務 - 401未授權
- 27. WCF服務返回另一個服務(服務工廠?)
- 28. (Silverlight)返回LinkedList的WCF服務
- 29. WCF服務 - JSON - 沒有數據返回
- 30. wcf ria服務方法返回
正在做[OperationContract,WebInvoke] [Service]的東西? – 2011-12-08 00:38:37