【发布时间】:2017-10-05 20:24:06
【问题描述】:
我阅读了很多关于如何发送客户端证书的帖子并做了所有这些,但它在服务器端是空的。
我在 mytest.aspx.cs 页面上编写了这段代码
protected void Page_Load(object sender, EventArgs e)
{
string host = @"http://localhost:57855/Temp/index.aspx";
string certName = @"C:\cert.pfx";
string password = @"123456";
try
{
X509Certificate2Collection certificates = new
X509Certificate2Collection();
certificates.Import(certName, password,
X509KeyStorageFlags.MachineKeySet |
X509KeyStorageFlags.PersistKeySet);
ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(host);
req.AllowAutoRedirect = true;
req.ClientCertificates = certificates;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string postData = "login-form-type=cert";
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
req.ContentLength = postBytes.Length;
Stream postStream = req.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
{
string line = reader.ReadLine();
while (line != null)
{
Console.WriteLine(line);
line = reader.ReadLine();
}
}
stream.Close();
}
catch (Exception ex)
{
//Console.WriteLine(e);
}
}
在 index.aspx 页面中我写了这段代码
protected void Page_Load(object sender, EventArgs e)
{
bool b = false;
if (HttpContext.Current.Request.ClientCertificate.IsPresent)
b = true;//b is always null
}
我也在使用 IIs express 。在 C:\Users\Administrator\Documents\IISExpress\config 的 applicationhost 文件中,我更改了两部分
<security>
<access sslFlags="SslNegotiateCert" />
....
<authentication>
<clientCertificateMappingAuthentication enabled="true" />
<iisClientCertificateMappingAuthentication enabled="true">
</iisClientCertificateMappingAuthentication>
.........
</security>
我在 mmc=>Certificates/personal/certificates 中安装了 cert.pfx 和 mmc=>证书(当前用户)/个人/证书
但总是在索引页 b 为假。
另外我应该说 cert.pfx 不是 ssl 证书。它是一个数字签名证书,并且在证书的 enhanskeyusage 字段中具有客户端身份验证
【问题讨论】:
-
为什么要在代码中加载 ssl 证书?你想使用 https 吗?
-
我想用证书对客户端进行身份验证。我需要客户在他的 http/https 请求中附加一个证书(ssl 或数字签名证书......)
标签: c# certificate client iis-express client-certificates