Advertisement
Guest User

Untitled

a guest
Nov 1st, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. HttpTransportBindingElement httpBindingElement = new HttpTransportBindingElement();
  2. httpBindingElement.AuthenticationScheme = AuthenticationSchemes.Basic;
  3. ...
  4. PTZClient ptzClient = new PTZClient(customBinding, endPointAddress);
  5. ptzClient.Endpoint.Behaviors.Remove(typeof(System.ServiceModel.Description.ClientCredentials));
  6. UsernameClientCredentials onvifCredentials = new UsernameClientCredentials(new UsernameInfo(_username, _password));
  7. ptzClient.Endpoint.Behaviors.Add(onvifCredentials);
  8. ptzClient.ClientCredentials.UserName.UserName = _username;
  9. ptzClient.ClientCredentials.UserName.Password = _password;
  10.  
  11. TransportSecurityBindingElement transportSecurity = new TransportSecurityBindingElement();
  12. // UsernameCredentials is a class implementing WS-UsernameToken authentication
  13. transportSecurity.EndpointSupportingTokenParameters.SignedEncrypted.Add(new UsernameTokenParameters());
  14. transportSecurity.AllowInsecureTransport = true;
  15. transportSecurity.IncludeTimestamp = false;
  16. TextMessageEncodingBindingElement messageEncoding = new TextMessageEncodingBindingElement(MessageVersion.Soap12, Encoding.UTF8);
  17. HttpClientCredentialType[] credentialTypes = new HttpClientCredentialType[3] { HttpClientCredentialType.None, HttpClientCredentialType.Basic, HttpClientCredentialType.Digest };
  18. ...
  19. foreach (HttpClientCredentialType credentialType in credentialTypes)
  20. {
  21. BasicHttpBinding httpBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
  22. httpBinding.Security.Transport.ClientCredentialType = credentialType;
  23. BindingElementCollection elements = new BindingElementCollection(new BindingElement[1]{messageEncoding});
  24. foreach(BindingElement element in httpBinding.CreateBindingElements())
  25. {
  26. if (element is TextMessageEncodingBindingElement)
  27. continue;
  28. elements.Add(element);
  29. }
  30. CustomBinding customBinding = new CustomBinding(elements);
  31. DeviceClient deviceClient = new DeviceClient(customBinding, endPointAddress);
  32. if (credentialType == HttpClientCredentialType.Basic)
  33. {
  34. // Set all credentials, not sure from which one WCF actually takes the value
  35. deviceClient.ClientCredentials.UserName.UserName = pair[0];
  36. deviceClient.ClientCredentials.UserName.Password = pair[1];
  37. }
  38. else if (credentialType == HttpClientCredentialType.Digest)
  39. {
  40. deviceClient.ClientCredentials.HttpDigest.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
  41. deviceClient.ClientCredentials.HttpDigest.ClientCredential.UserName = pair[0];
  42. deviceClient.ClientCredentials.HttpDigest.ClientCredential.Password = pair[1];
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement