2010-02-05 6 views
7

J'ai un service Web WCF et un client sur la même machine. L'accès au service Web WCF directement à l'aide du navigateur fonctionne, mais le client ne peut pas se connecter; message d'erreur ci-dessous. Des idées? L'authentification Windows intégrée dans IIS est utilisée à la fois pour le client et le serveur.(401) Erreur non autorisée: sécurité/liaison WCF

The remote server returned an error: (401) Unauthorized. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Net.WebException: The remote server returned an error: (401) Unauthorized. 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 

[WebException: The remote server returned an error: (401) Unauthorized.] 
    System.Net.HttpWebRequest.GetResponse() +5313085 
    System.ServiceModel.Channels.HttpChannelRequest.WaitForReply(TimeSpan timeout) +54 

[MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'Negotiate,NTLM'.] 
    System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +7594687 
    System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +275 
    HRPaysService.IService1.GetAlert() +0 
    HRPaysService.Service1Client.GetAlert() +15 
    _Default.Page_Load(Object sender, EventArgs e) +138 
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 
    System.Web.UI.Control.OnLoad(EventArgs e) +99 
    System.Web.UI.Control.LoadRecursive() +50 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627 

Client:

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="basicBinding"> 
       <security mode="TransportCredentialOnly"> 
        <transport clientCredentialType="Windows" 
          proxyCredentialType="Windows" realm="" /> 
        <message clientCredentialType="UserName" 
          algorithmSuite="Default" /> 
       </security> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint 
      address="http://hrpaysservice/service1.svc" 
      binding="basicHttpBinding" 
      bindingConfiguration="basicBinding" 
      contract="HRPaysService.IService1"> 
     </endpoint> 
    </client> 
    </system.serviceModel> 

Serveur:

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="basicBinding"> 
      <security mode="TransportCredentialOnly"> 
       <transport clientCredentialType="Windows" 
          proxyCredentialType="Windows" realm="" /> 
       <message clientCredentialType="UserName" 
         algorithmSuite="Default" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint 
      address="http://hrpaysservice/service1.svc" 
      binding="basicHttpBinding" 
      bindingConfiguration="basicBinding" 
      contract="HRPaysService.IService1"> 
     </endpoint> 
</client> 
</system.serviceModel> 
+0

est votre client une application Silverlight par hasard ?? Ceux-ci fonctionnent très différemment d'une application ASP.NET ou Winforms/WPF. –

+0

Non, pas une application Silverlight. –

Répondre

1

Client:

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
       <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" 
        textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> 
         <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
         <reliableSession ordered="true" inactivityTimeout="00:10:00" 
         enabled="false" /> 
        <security mode="Message"> 
         <transport clientCredentialType="Windows" proxyCredentialType="None" 
         realm="" /> 
         <message clientCredentialType="Windows" negotiateServiceCredential="true" 
         algorithmSuite="Default" establishSecurityContext="true" /> 
        </security> 
       </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:3097/Service1.svc" binding="wsHttpBinding" 
      bindingConfiguration="WSHttpBinding_IService1" contract="HRPaysService.IService1" 
      name="WSHttpBinding_IService1"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
     </endpoint> 
    </client> 
</system.serviceModel> 

Serveur:

<system.serviceModel> 
     <bindings> 
     <basicHttpBinding> 
        <binding name="basicBinding"> 
        <security mode="TransportCredentialOnly"> 
          <transport clientCredentialType="Windows"/> 
        </security> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
    <services> 
      <service behaviorConfiguration="basicBehavior" name="WcfService1.Service1"> 
       <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1" bindingConfiguration="basicBinding" /> 
       <endpoint address="mex" binding="basicHttpBinding" contract="IMetadataExchange" bindingConfiguration="basicBinding" /> 
     </service> 
    </services> 
    <behaviors> 
      <serviceBehaviors> 
       <behavior name="basicBehavior"> 
        <serviceMetadata httpGetEnabled="true" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
</system.serviceModel> 

+2

Pourquoi cela résout-il le problème? Qu'avez-vous changé? – Gusdor

0

Avez-vous un document Crossdomain.xml mis en place dans votre application web services? Sinon, créez un avec le contenu suivant -

<?xml version="1.0"?> 
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> 
<cross-domain-policy> 
    <allow-http-request-headers-from domain="*" headers="*"/> 
</cross-domain-policy> 
+0

Aucune idée de ce que cela fait? Je suis sur le même domaine. –

0

Si le répertoire virtuel des services WCF est pas configuré pour accès anonyme, le point d'extrémité "mex" doit être supprimé.

Vous avez posté 2 ensembles de configs différents et il semble y avoir une discordance. Pourriez-vous poster les configs qui causent l'erreur?

Votre première configuration de client (la plus haute) et la dernière configuration de serveur (sans la partie mex) devraient fonctionner.

3

J'ai rencontré la même erreur lorsque j'ai essayé d'accéder à un service WCF hébergé sur IIS en ajoutant un "Service Reference" à mon application Windows Forms. Mais lorsque le client a lancé un appel pour une méthode de service, j'ai reçu l'exception «Unauthorized 401». Voici ma solution à ce problème:

(1) J'utilisais [wsHttpBinding] passer à être [basicHttpBinding] comme suit dans le fichier de configuration de service WCF:

<system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpEndpointBinding"> 
        <security mode="TransportCredentialOnly"> 
         <transport clientCredentialType="Windows" /> 
        </security> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
     <services> 
     <service behaviorConfiguration="ServiceBehavior" name="IService1"> 
      <endpoint address="" binding="basicHttpBinding" 
      bindingConfiguration="BasicHttpEndpointBinding" 
      name="BasicHttpEndpoint" contract="IService1"> 
       <identity> 
        <dns value="localhost" /> 
       </identity> 
      </endpoint> 
      <endpoint address="mex" binding="mexHttpBinding" 
       contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
    multipleSiteBindingsEnabled="true" /> 

(2) Ajouter un « service de référence » de votre application client et lui donner un nom (nous utiliserons ce nom dans l'étape suivante comme « ProxyCalssName »)

(3) ajuster le fichier app.config de l'application cliente comme suit:

<system.serviceModel> 
    <client> 
     <endpoint address="your service URL" 
      binding="basicHttpBinding" bindingConfiguration="basic" contract="ProxyClassName.ServiceName" 
      name="default" /> 
    </client> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="basic"> 
       <security mode="TransportCredentialOnly"> 
        <transport clientCredentialType="Windows" proxyCredentialType="None" 
         realm="" /> 
        <message clientCredentialType="UserName" algorithmSuite="Default" /> 
       </security> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
</system.serviceModel> 

(4) Dans le code sous-jacent du client Application:

 ProxyClassName.MyServiceName srv = new ProxyClassName.MyServiceName("default"); 
//default is the name of the endpoint in the app.config file as we did. 
    srv.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation; 

Bonne chance, DigitalFox

+0

Merci pour votre réponse.Le code dans l'étape (4) est destiné à emprunter l'identité de l'authentification Windows en cours, qui correspond à la question d'origine. Cependant, je cherchais à accéder à un service WCF distant; votre configuration client fonctionnait très bien, mais le code a été modifié pour utiliser AllowedImpersonationLevel = TokenImpersonationLevel.Delegation et la mise en srv.ClientCredentials.Windows.ClientCredential.Domain/.Username/.Password aux valeurs appropriées. – Emanuel

+0

Après avoir essayé de faire fonctionner wsHttpBinding après plusieurs heures, j'ai finalement abandonné et essayé cette suggestion et j'ai finalement obtenu le service à travailler. J'aimerais savoir ce qui ne va pas avec wsHttpBinding, cependant, puisque je le comprends, il ne devrait pas avoir de problèmes. – Prethen

Questions connexes