Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. new ServiceReference1.Service1SoapClient().HelloMom("Bob");
  2.  
  3. class Program
  4. {
  5. private static bool customValidation(object s, X509Certificate c, X509Chain ch, SslPolicyErrors e)
  6. { return true }
  7.  
  8. static void Main(string[] args)
  9. {
  10. // accept anything
  11. ServicePointManager.ServerCertificateValidationCallback +=
  12. new RemoteCertificateValidationCallback(customValidation);
  13.  
  14. var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
  15. binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
  16. binding.Security.Transport.Realm = "Secured area";
  17.  
  18. // the generated Web Service Reference class
  19. var client = new ServiceReference1.Service1SoapClient(
  20. binding,
  21. new EndpointAddress("https://smplsite.com/sandbox3/Service1.asmx")
  22. );
  23.  
  24. client.ClientCredentials.UserName.UserName = "testapp";
  25. client.ClientCredentials.UserName.Password = "testpw";
  26.  
  27. Console.WriteLine(client.HelloMom("Bob"));
  28. }
  29. }
  30.  
  31. MyWebService svc = new MyWebService();
  32. svc.Credentials = new System.Net.NetworkCredential(UserID, pwd);
  33. bool result = svc.MyWebMethod();
  34.  
  35. public class MyWebService : System.Web.Services.WebService
  36. {
  37. public AuthenticationHeader AuthenticationInformation;
  38.  
  39. public class AuthenticationHeader : SoapHeader
  40. {
  41. public string UserName;
  42. public string Password;
  43. }
  44.  
  45. [WebMethod( Description = "Sample WebMethod." )]
  46. [SoapHeader( "AuthenticationInformation" )]
  47. public bool MyWebMethod()
  48. {
  49. if ( AuthenticationInformation != null )
  50. {
  51. if ( IsUserAuthenticated( AuthenticationInformation.UserName,
  52. AuthenticationInformation.Password, ref errorMessage ) )
  53. {
  54. // Authenticated, do something
  55. }
  56. else
  57. {
  58. // Failed Authentication, do something
  59. }
  60. }
  61. else
  62. {
  63. // No Authentication, do something
  64. }
  65. }
  66. }
  67.  
  68. MyWebService svc = new MyWebService();
  69. svc.AuthenticationHeaderValue = new MyWebService.AuthenticationHeader();
  70. svc.AuthenticationHeaderValue.UserName = UserID;
  71. svc.AuthenticationHeaderValue.Password = Password;
  72.  
  73. bool result = svc.MyWebMethod();
  74.  
  75. <binding name="MyBinding" closeTimeout="00:00:30"
  76. openTimeout="00:00:30" receiveTimeout="00:00:30" sendTimeout="00:00:30"
  77. allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
  78. maxBufferPoolSize="524288" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
  79. textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"
  80. messageEncoding="Text">
  81. <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
  82. maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
  83. <security mode="Transport">
  84. <transport clientCredentialType="Basic" realm=""/>
  85. </security>
  86. </binding>
  87. </basicHttpBinding>
  88.  
  89. proxy.ClientCredentials.UserName.UserName = userName;
  90. proxy.ClientCredentials.UserName.Password = password;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement