代码之家  ›  专栏  ›  技术社区  ›  Jeldrik

httplistner:截获对wcf dataservice的请求

  •  0
  • Jeldrik  · 技术社区  · 16 年前

    我想使用.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基本身份验证?我不能通过网络浏览器的登录对话框进行身份验证。

    多谢,
    杰霍

    1 回复  |  直到 12 年前
        1
  •  -1
  •   nitzmahone    12 年前

    通过httplistener进行代理时,你找错了树。看一看 this .