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)
        {

        }

Comments

Popular posts from this blog

Embedding PowerBI with ASP.NET Core 2 and Angular(2,4,5)

Handling Exit Event of Console Application in C#

CSS text-overflow ellipsis in the beginning of the the text - Left Ellipsis using CSS in a Flex box