wtorek, 17 lipca 2012

Konfiguracja usługi WCF z uwierzytelnianiem Windows Authentication

Poniżej przedstawiam plik web.config dla usługi WCF działającej z uwierzytelnianiem Windows. To co trzeba zmienić w swojej usłudze to wartości atrybutów:
 <services>  
  <service   
   name="MyNamespace.MyService">  
   <endpoint contract="MyNamespace.IMyService"/>           
  </service>  
 </services>  
 
<?xml version="1.0"?>
<configuration>

  <connectionStrings>
    
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Windows"/>    
  </system.web>

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="Mybinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

    <services>
      <service behaviorConfiguration="MybehaviorConfig"
        name="MyNamespace.MyService">
        <endpoint address="" binding="basicHttpBinding"
                  bindingConfiguration="Mybinding"
          name="MyEndpoint" contract="MyNamespace.IMyService"/>                  
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MybehaviorConfig">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  </system.serviceModel>
  
</configuration>
 
Następnie trzeba ustawić uwierzytelnianie Windows w konfiguracji usługi na IIS. W IIS7 robimy to następująco:
WebSite - Nasza usługa - Authentication:
- wyłączyć Anonumous i wszystkie inne
- włączyć Windows Authentication Następnie kliknąć na Windows Authentication i dalej Providers:
dodać providera Negotiate,
ważna jest kolejność:
NTLM
Negotiate

W aplikacji klienta trzeba dodać poniższy kod aby aplikacja mogła prawidłowo się uwierzytelniać:
SSOService ws = new SSOService();          

ws.Credentials = System.Net.CredentialCache.DefaultCredentials;