2009-12-28 25 views

cevap

0

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(); 
+0

Tam olarak doğru değil ... Bunu xHttpBindings'in HERHANGİ BİRİ ile yapabilirsiniz. – WayneC

5

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 
1

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.)

2
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. "