【发布时间】:2011-01-20 11:33:23
【问题描述】:
我想使用 .net 类 HttpListener 来拦截对我的自托管 (WebServiceHost) WCF 数据服务的请求,以便将“WWW-Authenticate”标头添加到响应中(用于用户身份验证)。但似乎 HttpListener 没有拦截任何发送到我的数据服务的请求。 HttpListner 适用于不同的路径就好了。示例:
HttpListner 前缀:http://localhost/somePath/
有效:http://localhost/somePath/
无效:http://localhost/somePath/myWCFDataService
是否还可以使用 HttpListner 拦截转到自托管 WCF 数据服务 (WebServiceHost) 的请求?
下面是相关代码sn-ps...
托管 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 基本身份验证?我无法通过网络浏览器的登录对话框进行身份验证。
非常感谢,
耶和
【问题讨论】:
标签: wcf wcf-data-services basic-authentication httplistener