使用Web服务HTTPS(带证书进行身份验证)

时间:2012-12-13 11:11:47

标签: c# web-services windows-mobile ssl-certificate x509certificate

我正在尝试使用Web服务HTTPS(使用证书身份验证)。我在我的项目的Web Reference中添加了我的Web服务,并且“myCertificate.cer”已导入到Console Root \ Certificates \ Personal。

我有错误401:请求失败,HTTP状态为401:未经授权。

有什么想法吗?

....
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);
    }
}

抱歉我的英文。感谢!!!

0 个答案:

没有答案