Guest User

Untitled

a guest
Apr 21st, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. Client client = new Client(); // <- the wcf ClientBase implementation for the serivce.
  2. client.Endpoint.Behaviors.Remove<ClientCredentials>();
  3. client.Endpoint.Behaviors.Add<ClientCredentialsEx>();
  4. client.DisplayInitializationUI();
  5. client.InnerChannel.Open();
  6.  
  7. public class ClientCredentialsEx : ClientCredentials
  8. {
  9. public ClientCredentialsEx() : base() { }
  10. public ClientCredentialsEx(ClientCredentialsEx other) : base(other) { }
  11.  
  12. public override void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
  13. {
  14. behavior.InteractiveChannelInitializers.Add(new ShowCredentialUI());
  15. base.ApplyClientBehavior(serviceEndpoint, behavior);
  16. }
  17. protected override ClientCredentials CloneCore()
  18. {
  19. return new ClientCredentialsEx(this);
  20. }
  21. }
  22.  
  23. public IAsyncResult BeginDisplayInitializationUI(IClientChannel channel, AsyncCallback callback, object state)
  24. {
  25. string host = "";
  26. CREDUI_INFO info = new CREDUI_INFO();
  27. string username = string.Empty, password = string.Empty;
  28.  
  29. CREDUI_FLAGS flags = CREDUI_FLAGS.GENERIC_CREDENTIALS |
  30. CREDUI_FLAGS.SHOW_SAVE_CHECK_BOX |
  31. CREDUI_FLAGS.EXPECT_CONFIRMATION;
  32.  
  33. bool savePwd = false;
  34.  
  35. // PromptForCredentials calls interop credui to challenge the user for username/password/smartcard.
  36. CredUIReturnCodes result = PromptForCredentials(ref info, host, 0, ref username,
  37. ref password, ref savePwd, flags);
  38.  
  39.  
  40. ChannelParameterCollection collection = channel.GetProperty<ChannelParameterCollection>();
  41. collection.Add(new NetworkCredential(username, password));
  42. return new AsyncResult();
  43. }
Add Comment
Please, Sign In to add comment