尝试使用客户端证书进行身份验证时出现401.2错误

时间:2014-03-26 18:16:43

标签: c# httpclient client-certificates

我尝试使用来自.net应用程序的客户端证书对网站进行身份验证。

使用相同的证书我可以使用chrome和IE访问该网站。在同一台机器上,我得到错误状态码401.2。

这是我使用的代码:

X509Certificate2 cert2 = new X509Certificate2(x509Cert);
cert2.PrivateKey = algorithm;

if (cert2.Verify() && cert2.HasPrivateKey)
{
    WebRequestHandler handler = new WebRequestHandler();
    handler.ClientCertificates.Add(cert2);
    HttpClient client = new HttpClient(handler);

    string result = await client.GetStringAsync("https://localhost:8443/");
}

编辑:

我现在尝试从证书存储中提取浏览器使用的相同证书,但我仍然得到相同的错误,并且我已经切换到使用旧的WebClient,我再次得到相同的错误。

任何线索都会受到赞赏!

由于

罗斯

1 个答案:

答案 0 :(得分:0)

问题不在于我的代码,而是IIS配置问题。

浏览器正在运行,因为他们也在发送我的Windows凭据 - 我的应用程序没有这样做,这就是它失败的原因。

我必须启用将客户端证书映射到AD:http://www.iis.net/configreference/system.webserver/security/authentication/clientcertificatemappingauthentication

然后我不得不在网站上禁用Windows身份验证。然后该应用程序按预期工作。

相关问题