开发者

HttpListner: intercept requests to WCF DataService

开发者 https://www.devze.com 2022-12-20 22:29 出处:网络
I want to use the .net class HttpListener to intercept requests to my selfhosted (WebServiceHost) WCF Data Service in order to add the \"WWW-Authenticate\" header to the response (for user authenticat

I want to use the .net class HttpListener to intercept requests to my selfhosted (WebServiceHost) WCF Data Service in order to add the "WWW-Authenticate" header to the response (for user authentication). But it seems like that the HttpListener doesn't intercept any requests that go to my dataservice. The HttpListner works for different paths just fine. Example:

HttpListner Prefix: http://localhost/somePath/

Works: http://localhost/somePath/

Doesn't Work: http://localhost/somePath/myWCFDataService

Is it possible to intercept also requests that go to a selfhosted WCF Data Service (WebServiceHost) with the HttpListner?

Here are the relevant code snippets...

Hosting the WCF DataService:

WebServiceHost dataServiceHost = new WebServiceHost(typeof(MyWCFDataService));
WebHttpBinding binding = new WebHttpBinding();
dataServiceHost.AddServiceEndpoint(typeof(IRequestHandler), binding, 
    "http://localhost/somePath/myWCFDataService");
dataServiceHost.Open();

The HTTP Listner:

HttpListener httpListener = new HttpListener();
httpListener.Prefixes.Add("http://localhost/somePath/");
httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
httpListener.S开发者_开发百科tart();

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();
    }
}

Is there a better way for doing HTTP basic authentication within WCF Data Services? I wan't to be able to authenticate via the login dialog of the web browser.

Many thanks,

JeHo


You're barking up the wrong tree messing with proxying via HttpListener. Have a look at this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号