我正在尝试将客户端证书传递给POST请求,但 Request.ClientCertificate 属性为空(无证书)
这是我的代码(Program.cs)
class Program
{
static void Main(string[] args)
{
var cert = GetCertificate();
DoclinkWebClient client = new DoclinkWebClient(cert);
string urlToSend = "http://localhost:47635/Home/PostData";
ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
try
{
byte[] content = File.ReadAllBytes(@"D:\2528fac5e1734839b5068c225b493e7d.cms");
client.UploadData(urlToSend, "POST", content);
}
catch (WebException webException)
{
string additionalText = "";
if (webException.Response != null)
{
Stream stream = webException.Response.GetResponseStream();
if (stream != null)
{
additionalText = new StreamReader(stream).ReadToEnd();
}
}
throw new WebException(webException.Message + "." + additionalText);
}
}
}
DoclinkWebClient.cs
public class DoclinkWebClient : WebClient
{
private X509Certificate2 _certificate;
public DoclinkWebClient(X509Certificate2 certificate)
{
_certificate = certificate;
}
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.ClientCertificates.Add(_certificate);
return request;
}
}
证书有私钥(可以签名)
为什么Web应用程序端的Request.ClientCertificate为空?
[HttpPost]
public ActionResult PostData(FormCollection formCollection)
{
var clientCertificate = Request.ClientCertificate;
var raw = clientCertificate.Certificate;
//raw.Length = 0;
return null;
}
我错过了什么?