Consume Web Service HTTPS (with certificate for authentication)

微笑、不失礼 提交于 2019-12-10 11:03:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!