问题
I am trying to consume a Web Service HTTPS (using a certificate authentication). I have added my Web Service in my Web Reference on my project and "myCertificate.cer" has imported to Console Root\Certificates\Personal.
I have an error 401: The request failed with HTTP status 401: Unauthorized.
Any idea?
....
using System.Security.Cryptography.X509Certificates;
...
string certPath = @"C:\myCertificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(certPath);
// Create an instance of the Web service proxy.
zgetuser_certificate mathservice = new zgetuser_certificate();
// Create an X509Certificate object
mathservice.ClientCertificates.Add(X509Certificate.CreateFromCertFile(certPath));
try
{
ZgetUser user = new ZgetUser();
user.Username = "USER01";
ZgetUserResponse response = new ZgetUserResponse();
response = mathservice.ZgetUser(user); //ERROR: The request failed with HTTP status 401: Unauthorized.
Console.WriteLine("{0}, {1}, {2}, {3}", response.Company, response.Firstname, response.Fullname, response.Lastname);
}
catch (Exception ex)
{
if (ex is WebException)
{
WebException we = ex as WebException;
WebResponse webResponse = we.Response;
throw new Exception("Exception calling method. " + ex.Message);
}
}
Sorry for my English. Thanks!!!
来源:https://stackoverflow.com/questions/13858609/consume-web-service-https-with-certificate-for-authentication