2

我正在尝试使用 Web 服务 HTTPS(使用证书身份验证)。我已在我的项目的 Web 参考中添加了我的 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);
    }
}

对不起我的英语不好。谢谢!!!

4

0 回答 0