Question

Je veux utiliser la classe .net HttpListener pour intercepter les demandes à mon selfhosted (WebServiceHost) WCF Data Service afin d'ajouter l'en-tête « WWW-Authenticate » à la réponse (pour l'authentification utilisateur). Mais il semble que le HttpListener n'intercepte toutes les demandes qui vont à mon dataservice. Le HttpListner travaille pour différents chemins très bien. Exemple:
HttpListner Prefix: http: // localhost / somepath /
Travaux: < a href = "http: // localhost / somepath /" rel = "nofollow noreferrer"> http: // localhost / somepath /
ne fonctionne pas: http: // localhost / somepath / myWCFDataService

Est-il possible d'intercepter également des demandes qui vont à un selfhosted WCF Data Service (WebServiceHost) avec le HttpListner
Voici les extraits de code pertinents ...

Hébergement WCF DataService:

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

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

Y at-il une meilleure façon de faire l'authentification de base HTTP dans les services WCF Data? Je wan't pour être en mesure d'authentifier via la boîte de dialogue de connexion du navigateur Web.

Merci,
Jeho

Était-ce utile?

La solution

Vous êtes aboiements le mauvais arbre de jouer avec le mandatement par HttpListener. Jetez un oeil à cette .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top