我想使用.net类HttpListener拦截对我的自主(WebServiceHost)WCF数据服务的请求,以便将“WWW-Authenticate”标头添加到响应中(用于用户身份验证)。但似乎HttpListener不会拦截任何发送到我的dataservice的请求。 HttpListner可以很好地适用于不同的路径。示例:
HttpListner前缀:http://localhost/somePath/
作品:http://localhost/somePath/
不起作用:http://localhost/somePath/myWCFDataService
是否可以拦截使用HttpListner转到自主WCF数据服务(WebServiceHost)的请求?
以下是相关的代码片段......
托管WCF DataService:
WebServiceHost dataServiceHost = new WebServiceHost(typeof(MyWCFDataService));
WebHttpBinding binding = new WebHttpBinding();
dataServiceHost.AddServiceEndpoint(typeof(IRequestHandler), binding,
"http://localhost/somePath/myWCFDataService");
dataServiceHost.Open();
HTTP Listner:
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