C# 客户端证书在服务器端始终为空
我读了很多关于如何发送客户端证书的帖子,并且都做了,但是在服务器端它是空的 我在mytest.aspx.cs页面上写了这段代码C# 客户端证书在服务器端始终为空,c#,certificate,client,iis-express,client-certificates,C#,Certificate,Client,Iis Express,Client Certificates,我读了很多关于如何发送客户端证书的帖子,并且都做了,但是在服务器端它是空的 我在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 = @"1234
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和
mmc=>证书(当前用户)/个人/证书
但总是在索引页b是假的
另外,我应该说cert.pfx不是ssl证书。它是一个数字签名证书,在cert的enhanskeyusage字段中具有客户端身份验证我在服务器中安装了客户端证书吊销列表,它已解决为什么要在代码中加载ssl证书?您想使用https吗?我想使用证书进行客户端身份验证。我需要客户端随他的http/https请求(ssl或数字签名证书…)附上一个证书。我很难做到这一点,因此,如果您能确认这些,那就太好了:(1)客户端和服务器证书需要来自同一CA,(2)IIS应该将ssl设置切换为
接受。使用您的设置,“我的web应用”不会加载。我得到一个500 HTTP错误代码。这可以与表单身份验证结合使用吗?