Guest User

Untitled

a guest
May 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. public class HeaderAdder : ContextBoundObject, IClientMessageInspector
  2. {
  3. public bool RequestFailedDueToAuthentication;
  4.  
  5. public string UserName { get; set; }
  6. public string Password { get; set; }
  7.  
  8. public object BeforeSendRequest(ref Message request, IClientChannel channel)
  9. {
  10. var property = new UserNameHeader
  11. {
  12. Password = Password,
  13. UserName = UserName
  14. };
  15. request.Headers.Add(MessageHeader.CreateHeader("UserNameHeader", "test", property));
  16. return null;
  17. }
  18.  
  19. public void AfterReceiveReply(ref Message reply, object correlationState)
  20. {
  21. RequestFailedDueToAuthentication = reply.ToString().Contains("ErrorCode>-4<");
  22. }
  23. }
  24.  
  25. public class CustomEndpointBehavior : IEndpointBehavior
  26. {
  27. private readonly HeaderAdder _headerAdder;
  28.  
  29. public CustomEndpointBehavior(HeaderAdder headerAdder)
  30. {
  31. _headerAdder = headerAdder;
  32. }
  33.  
  34. public void Validate(ServiceEndpoint endpoint)
  35. {
  36. //throw new NotImplementedException();
  37. }
  38.  
  39. public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
  40. {
  41. //throw new NotImplementedException();
  42. }
  43.  
  44. public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
  45. {
  46. //throw new NotImplementedException();
  47. }
  48.  
  49. public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  50. {
  51. var credentials = endpoint.Behaviors.Find<ClientCredentials>();
  52. if (!string.IsNullOrEmpty(credentials.UserName.Password))
  53. {
  54. _headerAdder.UserName = credentials.UserName.UserName;
  55. _headerAdder.Password = credentials.UserName.Password;
  56. clientRuntime.ClientMessageInspectors.Add(_headerAdder);
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment