WCF hizmetinden bir HTTP 401'i nasıl iade edebilirim?WCF hizmetinden 401 döndürme
cevap
yetkilendirme kontrolü yapmak gerektiğinde bağlı olarak, böyle bir şey kullanarak bir HttpModule
yapabileceğini aşağıdadır:
HttpContext context = HttpContext.Current;
context.Response.StatusCode = 401;
context.Response.End();
Tam olarak doğru değil ... Bunu xHttpBindings'in HERHANGİ BİRİ ile yapabilirsiniz. – WayneC
siteyi o şekilde yapılabilir bir REST hizmet programlama yapıyorsanız:
private IWebOperationContext context = new WebOperationContextWrapper(WebOperationContext.Current); // Get the context
context.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized; // Set the 401
WCF REST Starter Kit WebServiceHost2 fabrika kullanıyorsanız, ayrıca belirli WebProtocolException
atmak ve bir HTTP dönüş kodu belirtebilirsiniz:
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
da 401 durum koduna karşılık gelen HttpStatusCode.Unauthorized
var. HTTP dönüş kodlarının belirtilmesiyle ilgili çeşitli yollar hakkında daha ayrıntılı bilgi için bkz. Rob Bagby'nin mükemmel blog yazısı olan Effective Error Handling with WCF REST. (Ekran Rob blog yazısı vardır - bunun için tüm hak ediyor.)
throw new WebFaultException(System.Net.HttpStatusCode.Unauthorized);
Notlar: "MSDN: Bir WCF REST son nokta (WebHttpBinding ve WebHttpBehavior veya WebScriptEnablingBehavior) yanıtı HTTP durum kodunu kullanırken Ancak, WebFaultException, REST olmayan son noktalarla kullanılabilir ve normal bir FaultException gibi davranır. "
[OperationContract, WebInvoke] [Hizmet] şeyi yapıyor musunuz? –