title: How Do I Correctly Use DisplayInitializationUI in WCF to Prompt the User for Credentials to Authenticate and use Web Services in a Different Domain?
body: <p>I have a WPF client that connects to a set of WCF web services. We have a Dev, Test, Volume and Production domains, with the services available on a server in each domain. The domains do not have trust relationships.</p>
  
  <p>The goal is to have the client connect the the services of any other domain. If the client and server are on the same domain then the Default Network Credentials should be sent to authenticate/authorize. If the server is in a different domain, then it should challenge the user for credentials, much in the same way Internet Explorer does when you hit a site on a different domain with Integrated Security set.</p>
  
  <p>At the moment i'm trying to fetch the clients credentials by setting  DisplayInitializationUI() when creating an instance of the ClientBase for my service.</p>
  
  <pre><code>Client client = new Client(); // &lt;- the wcf ClientBase implementation for the serivce.
  client.Endpoint.Behaviors.Remove&lt;ClientCredentials&gt;();
  client.Endpoint.Behaviors.Add&lt;ClientCredentialsEx&gt;();
  client.DisplayInitializationUI();
  client.InnerChannel.Open();
  </code></pre>
  
  <p>The ClientCredentialsEx class.</p>
  
  <pre><code>public class ClientCredentialsEx : ClientCredentials
  {
      public ClientCredentialsEx() : base() { }
      public ClientCredentialsEx(ClientCredentialsEx other) : base(other) { }
  
      public override void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
      {
          behavior.InteractiveChannelInitializers.Add(new ShowCredentialUI());
          base.ApplyClientBehavior(serviceEndpoint, behavior);
      }
      protected override ClientCredentials CloneCore()
      {
          return new ClientCredentialsEx(this);
      }
  }   
  </code></pre>
  
  <p>The ShowCredentialUI class implements IInteractiveChannelInitializer. The business end of the class is the BeginDisplayInitializationUI method;</p>
  
  <pre><code>public IAsyncResult BeginDisplayInitializationUI(IClientChannel channel, AsyncCallback callback, object state)
  {
      string host = "";
      CREDUI_INFO info = new CREDUI_INFO();
      string username = string.Empty, password = string.Empty;
  
      CREDUI_FLAGS flags = CREDUI_FLAGS.GENERIC_CREDENTIALS |
                           CREDUI_FLAGS.SHOW_SAVE_CHECK_BOX |
                           CREDUI_FLAGS.EXPECT_CONFIRMATION;
  
      bool savePwd = false;
  
      // PromptForCredentials calls interop credui to challenge the user for username/password/smartcard.
      CredUIReturnCodes result = PromptForCredentials(ref info, host, 0, ref username,
                                                        ref password, ref savePwd, flags);
  
  
      ChannelParameterCollection collection = channel.GetProperty&lt;ChannelParameterCollection&gt;();
      collection.Add(new NetworkCredential(username, password));
      return new AsyncResult();
  }
  </code></pre>
  
  <p>The problem is when I investigate the iis logs, the credentials are never sent. I suspect that I am missing something crucial. Has anyone had a similar scenario implemented successfully? Is there an easier way to achieve this?</p>
  
  <p>Thanks for reading!</p>