0

我想使用 .net 类 HttpListener 来拦截对我的自托管 (WebServiceHost) WCF 数据服务的请求,以便将“WWW-Authenticate”标头添加到响应中(用于用户身份验证)。但似乎 HttpListener 没有拦截任何发送到我的数据服务的请求。HttpListner 适用于不同的路径就好了。示例:

HttpListner 前缀:http://localhost/somePath/ 有效
http://localhost/somePath/
无效:http://localhost/somePath/myWCFDataService

是否也可以使用 HttpListner 拦截前往自托管 WCF 数据服务 (WebServiceHost) 的请求?
以下是相关的代码片段...

托管 WCF 数据服务:

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 浏览器的登录对话框进行身份验证。

非常感谢,
杰霍

4

1 回答 1

-1

您正在通过 HttpListener 搞乱代理的错误树。看看这个

于 2010-02-26T04:10:37.680 回答