我正在测试我希望用来发布的aspx页面,但我似乎无法通过数据发送证书。这是我的客户代码:
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string postData = "age=23&state=47";
byte[] data = encoding.GetBytes(postData);
X509Certificate x509certificate = X509Certificate.CreateFromCertFile(@"C:\Documents and Settings\rcimbalo\My Documents\Downloads\SSL Certificates\testCert.cer");
string key = x509certificate.GetPublicKeyString();
string certData = Encoding.ASCII.GetString(x509certificate.Export(X509ContentType.Cert));
string pks12 = Encoding.ASCII.GetString(x509certificate.Export(X509ContentType.Pkcs12));
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost:3493/Index.aspx");
myRequest.Credentials = new NetworkCredential("xxxx", "yyyy");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
myRequest.ClientCertificates.Add(x509certificate);
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
System.IO.StreamReader st = new StreamReader(((HttpWebResponse)myRequest.GetResponse()).GetResponseStream());
Console.Write(st.ReadLine());
这是我的aspx页面中的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (this.CheckCredentials())
{
string data = this.Context.Request.Form.ToString();
}
}
private bool CheckCredentials()
{
string userName = User.Identity.Name;
HttpClientCertificate certificate = Request.ClientCertificate;
if (certificate.IsPresent)
{
string w = certificate.Get("SUBJECT 0");
return true;
}
return false;
}
永远不会有证书。
我之前发现了这个帖子 X509Certificate not getting transmitted to the server
但是不明白答案。我使用MakeCert.exe实用程序创建了一个证书,但我真的不明白为什么它不起作用。
答案 0 :(得分:2)
客户端证书通过SSL connection(https:// ....)发送。内置的asp.net开发服务器无法做到这一点,但如果您使用新的IIS Express或本地IIS实例,则可以实现。