【问题标题】:Make HttpRequest With Certificates使用证书制作 HttpRequest
【发布时间】:2015-05-27 21:05:20
【问题描述】:

所以,我与网络服务提供商建立了 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);

【问题讨论】:

  • 很明显,您是否将 https 指定为您正在调用的 URL 的协议?
  • 另外,您是否在证书存储中安装了私钥/公钥对?私钥将用于在 SSL 握手期间生成哈希。服务器将使用您提供的证书中的公钥验证该哈希。这样,服务器可以确定您拥有私钥,而不是持有公共证书并试图冒充的人。 IE。这就是您需要 .pfx 文件的原因 - 您需要将其安装到您的证书存储中。
  • 在您的网络浏览器中是否可以使用相同的 URL?
  • 所以,现在我也安装了.pfx 文件,并附上了(查看更新)
  • 请求仍然以 403 结束。但是现在我在机器上安装了两个证书(pfx,cer)。

标签: c# web-services certificate vpn access-denied


【解决方案1】:

查看这篇 Microsoft 支持文章。它有这方面的代码示例和几种不同的证书处理方式

How to send a client certificate by using the HttpWebRequest and HttpWebResponse classes in Microsoft Visual C# .NET

您似乎缺少证书策略部分。

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 Explorer 下载的。

//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 工具。

【讨论】:

  • 我认为您缺少证书策略,如果您添加该部分是否有效?
  • 不,它仍然没有。后来我把它去掉了。
  • 一般来说.pfx也需要安装吗?
  • 查看关于需要 .pfx 文件的问题下的评论
  • 运行时不需要 .pfx,但他需要使用它来将私钥/公钥对安装到他的证书存储中,因为从问题中可以看出他没有生成初始私有/ 公钥对,而是提供给他。
猜你喜欢
  • 2010-10-26
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多