2010-02-24 129 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