简体   繁体   中英

Acessing a WebService (HTTPS) that requires a certificate via WCF in C#

I'm the client machine, and the server I'm trying to access is a program which I'll be providing support to (I'm a new 3rd party program) and I need to send then some data.

They use HTTPS but I couldn't connect with this:

public void StartProcess() 
{
    BasicHttpsBinding binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
    // - They said they don't require credentials

    EndpointAddress address = new EndpointAddress("https://www.outsideservice.com/services/C001");

    OutsideServiceClient client = new OutsideServiceClient(binding, address);

    DataToSend data = new DataToSend();
    // - Filling up whatever data I need to send, omitted

    try
    {
        client.StartProcess(data);
    }
    catch (Exception ex)
    {
        // ex = Could not establish trust relationship for SSL/TLS secure channel
    }
}

They already have other 3rd party programs accessing their servers. I don't have a very good knowledge on how certificates work (other than the basic reading on MSDN ).

After questioning how I can access their servers, I got the reply: " already access our servers via https, just access the machine, download the certificate and use on your client machine."

After googling around how I do this, I tried to access https://theirURL.com/certsrv from here , but I get Resource Not Found error.

From what I could understand from how certificates work, I must obtain a certificate from them, double click it, install on my machine (or the machine running the code to connect to them) to be able to connect right? Or is there something that I have to do on my end to be able to connect?

"I must obtain a certificate from them, double click it, install on my machine (or the machine running the code to connect to them) to be able to connect right?"

This is the correct way to do things, however to get things up and running you can use this bit of code:

System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

I usually like to wrap this in code that doesn't apply this on a release build so that production actually has to have the certificate installed properly as follows:

#if (!Release)
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
#endif

I believe you should be able to put this anywhere you like, such as the Global.asax.cs or right before the call is made.

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