views:

280

answers:

1

Hello,

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

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

A: 

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

nitzmahone
Ok, that seems to work. Thanks!