2010-02-24 127 views
0

我想使用的.NET類HttpListener攔截請求我selfhosted(WebServiceHost)WCF數據服務,以便將「WWW驗證」頭添加到響應(用於用戶認證) 。但是看起來好像HttpListener不會攔截到我的數據服務的任何請求。 HttpListner適用於不同的路徑。例如:

HttpListner前綴:http://localhost/somePath/
作品:http://localhost/somePath/
不起作用:http://localhost/somePath/myWCFDataService
HttpListner:攔截請求WCF DataService的

是否有可能也攔截去selfhosted WCF數據服務(WebServiceHost)與HttpListner要求?
下面是相關的代碼片段...

託管的WCF DataService的:

WebServiceHost dataServiceHost = new WebServiceHost(typeof(MyWCFDataService)); 
WebHttpBinding binding = new WebHttpBinding(); 
dataServiceHost.AddServiceEndpoint(typeof(IRequestHandler), binding, 
    "http://localhost/somePath/myWCFDataService"); 
dataServiceHost.Open(); 

的HTTP聽者:

HttpListener httpListener = new HttpListener(); 
httpListener.Prefixes.Add("http://localhost/somePath/"); 
httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous; 
httpListener.Start(); 

while (true) 
{ 
    HttpListenerContext context = httpListener.GetContext(); 
    string authorization = context.Request.Headers["Authorization"]; 

    if (string.IsNullOrEmpty(authorization)) 
    { 
     context.Response.StatusCode = 401; 
     context.Response.AddHeader("WWW-Authenticate", "Basic realm=\"myDataService\""); 
     context.Response.OutputStream.Close(); 
     context.Response.Close(); 
    } 
} 

是否有內WCF做HTTP基本身份驗證的更好的方法數據服務?我無法通過Web瀏覽器的登錄對話框進行身份驗證。

非常感謝,
JeHo

回答

-1

你找錯了樹通過HttpListener代理搞亂。看看this

+0

好吧,這似乎工作。謝謝! – Jeldrik 2010-02-26 09:39:24

+0

感謝誰的downvote,而不是固定,改變網址... – nitzmahone 2014-02-19 21:49:15

+0

對不起,我還以爲沒發表評論。感謝在殼體固定的鏈接,但請提供每[FAQ]上下文(http://stackoverflow.com/help/how-to-answer)它再次移動。 – roufamatic 2014-02-21 19:29:13