WCF Client with HTTPS enabled and Authentication
Following code sample shows how WCF client can be used with WCF service running on HTTPS and secure with authentication.
// Create the binding.
WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.Transport;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
// Create the endpoint address.
EndpointAddress ea = new EndpointAddress("https://www.mywcfservice.com/testwcf.svc");
// Create the client.
TestServiceClient cc = new TestServiceClient(myBinding, ea);
// The client must provide a user name and password.
cc.ClientCredentials.UserName.UserName = "uname";
cc.ClientCredentials.UserName.Password = "pwd";
try
{
// Begin using the client.
cc.Open();
cc.DoSomething();
// Close the client.
cc.Close();
}
catch (Exception ex)
{
}
// Create the binding.
WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.Transport;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
// Create the endpoint address.
EndpointAddress ea = new EndpointAddress("https://www.mywcfservice.com/testwcf.svc");
// Create the client.
TestServiceClient cc = new TestServiceClient(myBinding, ea);
// The client must provide a user name and password.
cc.ClientCredentials.UserName.UserName = "uname";
cc.ClientCredentials.UserName.Password = "pwd";
try
{
// Begin using the client.
cc.Open();
cc.DoSomething();
// Close the client.
cc.Close();
}
catch (Exception ex)
{
}
Comments
Post a Comment