WCF + https + Windows Authentication – get Self Account Credential

WCF + https + Windows Authentication – get Self Account Credential
 

 
Configuration : Deploy Both Application ( WCF & SOAP Client Web Application ) with https.
 
Enable Windows Authentication & ASP.NET Impersonation on both Web Application.

   WCF – web.conf
 
   <?xml version="1.0" encoding="UTF-8"?>
 
   <configuration>
 
      … …
 
      <system.web>
 
         … …
 
         <identity impersonate="true" />
 
      </system.web>
 
      <system.serviceModel>
 
         <services>
            <service name="WCFService.Service" behaviorConfiguration="soapbehavior">
               <endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpBinding" contract="WCFService.IService" />
            </service>
         </services>
 
         <bindings>
            <basicHttpBinding>
               <binding name="httpBinding">
                  <security mode="Transport">
                     <transport clientCredentialType="Windows" />
                     <message clientCredentialType="Certificate" />
                  </security>
               </binding>
            </basicHttpBinding>
         </bindings>
 
         <behaviors>
            <serviceBehaviors>
               <behavior name="soapbehavior">
                  <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                  <serviceDebug includeExceptionDetailInFaults="true" />
                  <serviceAuthorization impersonateCallerForAllOperations="false" />
               </behavior>
            </serviceBehaviors>
         </behaviors>
 
         <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
 
      </system.serviceModel>
 
      … …
 
   </configuration>
 
   WCF – IService.cs
 
   [ServiceContract]
   public interface IService
   {
 
      … …
 
      [OperationContract]
      string GetName();
 
      … …
 
   }
 
   WCF – Service.svc.cs
 
   using System;
   using System.ServiceModel;
 
   
   public class Service : IService
   {
 
      … …
 
      [OperationBehavior(Impersonation = ImpersonationOption.Required)]
      public string GetName()
      {
         return string.Format(ServiceSecurityContext.Current.WindowsIdentity.Name.ToString());
      }
 
      … …
 
   }
 
   SOAP Client Side Web Apps – Default.aspx.cs
 
   using System;
   using System.Web;
   using System.Web.UI;
   using System.Web.UI.WebControls;
   using WebApplication1.ServiceReference;
 
   protected void Page_Load(object sender, EventArgs e)
   {
      ServiceClient client = new ServiceClient();
 
      client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
      client.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
 
      this.UserName.Text = client.GetName();
   }
 
   SOAP Client Side Web Apps – web.conf
 
   <?xml version="1.0" encoding="utf-8"?>
 
   <configuration>
 
      … …
 
      <system.web>
 
         … …
 
         <authentication mode="Windows" />
 
         … …
 
         <identity impersonate="true" />
 
      </system.web>
 
      <system.serviceModel>
 
         <bindings>
            <basicHttpBinding>
               <binding name="BasicHttpBinding_IService">
                  <security mode="Transport">
                     <transport clientCredentialType="Windows" />
                     <message clientCredentialType="Certificate" />
                  </security>
               </binding>
            </basicHttpBinding>
         </bindings>
 
         <client>
            <endpoint address="https://vmw12002/Service.svc" binding="basicHttpBinding"
               bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference.IService"
               name="BasicHttpBinding_IService" />
         </client>
 
      </system.serviceModel>
 
   </configuration>