所以,我和网络服务提供商有VPN连接。我还得到了.cer和.pfx文件。我已经在本地的机器上安装了.cer。现在我正在尝试下载WSDL文件,但是每当我制作一个HttpRequest时,我就会得到403 (访问被拒绝)。
X509Certificate Cert = X509Certificate.CreateFromCertFile("path");
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("url");
Request.ClientCertificates.Add(Cert);
Request.Method = "GET";
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();我的执行有什么问题吗?
顺便问一下,为什么我需要.pfx?不仅.Cer文件足以提出请求吗?
更新
X509Certificate Cert = new X509Certificate();
Cert.Import(@"test.pfx", "123456", X509KeyStorageFlags.PersistKeySet);发布于 2015-03-24 14:14:48
请查看这篇Microsoft支持文章。它有这方面的代码示例和几种处理证书的不同方法。
如何使用Microsoft Visual C# .NET中的HttpWebResponse和C#类发送客户端证书
看起来您丢失了证书策略部分。
ServicePointManager.CertificatePolicy = new CertPolicy();
//Implement the ICertificatePolicy interface.
class CertPolicy: ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
{
// You can do your own certificate checking.
// You can obtain the error values from WinError.h.
// Return true so that any certificate will work with this sample.
return true;
}
}还要确保cer文件的路径是正确的,并且您使用Internet资源管理器下载了它。
//You must change the path to point to your .cer file location.
X509Certificate Cert = X509Certificate.CreateFromCertFile("C:\\mycert.cer");和
检查权限-
必须将ASP.NET用户帐户权限授予客户端证书的私钥。若要将ASP.NET用户帐户权限授予客户端证书的私钥,请使用WinHttpCertCfg.exe工具。
https://stackoverflow.com/questions/29234571
复制相似问题