简体   繁体   中英

How to work with WCF wsHttpBinding and SSL?

I need to develop a WCF Hosted in a console app WebService . I made it work using the Mutual Certificate (service and client) method using SecurityMode.Message . But now i need to change the Security Mode to SecurityMode.Transport and use the wsHttpBinding with SSL. I made this code to host the service but i cannot get the wsdl with the browser, or execute some webmethod in the console app client.

static void Main()
{
    var httpsUri = new Uri("https://localhost:8089/HelloServer");
    var binding = new WSHttpBinding();

    binding.Security.Mode = SecurityMode.Transport;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

    var host = new ServiceHost(typeof(WcfFederationServer.HelloWorld), httpsUri);
    host.AddServiceEndpoint(typeof(WcfFederationServer.IHelloWorld), binding, "", httpsUri);
    var mex = new ServiceMetadataBehavior();
    mex.HttpsGetEnabled = true;
    host.Description.Behaviors.Add(mex);

    // Open the service.
    host.Open();
    Console.WriteLine("Listening on {0}...", httpsUri);
    Console.ReadLine();

    // Close the service.
    host.Close();
}

The service is up, but i cannot get nothing on the https://localhost:8089/HelloServer . On fiddler the get request via browser shows me this message:

fiddler.network.https> HTTPS handshake to localhost failed. System.IO.IOException 

What im missing here? Thanks

EDIT:

The Console Application Client Code

 static void Main()
    {
        try
        {
            var client = new HelloWorldHttps.HelloWorldClient();
            client.ClientCredentials.ClientCertificate.SetCertificate(
                                            StoreLocation.LocalMachine,
                                            StoreName.TrustedPeople,
                                            X509FindType.FindBySubjectName,
                                            "www.client.com");

            Console.WriteLine(client.GetData());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadKey();
    }

Getting this error:

Could not establish trust relationship for the SSL/TLS secure channel

When it comes to the service, you need to map the certificate to the specific port as described here

http://msdn.microsoft.com/en-us/library/ms733791(v=vs.110).aspx

As for the client, you need to skip the verification of certificate properties like valid date, the domain by relaxing the certificate acceptance policy. An easiest way would be to accept any certiticate

 ServicePointManager.ServerCertificateValidationCallback = (a,b,c,d) => true 

You can finetune the acceptance callback according to the docs to best fit your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM