附加客户端证书时出现 Forbidden 403 错误

发布于 2024-12-13 23:59:48 字数 1009 浏览 2 评论 0原文

我正在消费一些服务并消费服务提供商已给出的证书。

因此,我已在 LocalMachine 上安装了证书,并通过以下代码将证书附加到我发布的 Web 请求中,以获取 Web 服务的响应。

X509Certificate cert = null;
string ResponseXml = string.Empty;

// Represents an X.509 store, which is a physical store
// where certificates are persisted and managed 
X509Store certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);

certStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection results =
    certStore.Certificates.Find(X509FindType.FindBySubjectDistinguishedName,
                                Constants.CertificateName, false);
certStore.Close();

if (results != null && results.Count > 0)
    cert = results[0];
else
{
    ErrorMessage = "Certificate not found";
    return ErrorMessage;
}
webClient.TransportSettings.ClientCertificates.Add(cert);

当我使用 ASP.net Cassini(ASP.NET 开发服务器)运行代码时,这非常有效。

但是当我在 IIS 7.0 中托管此代码时,它会给出 forbidden 403 错误作为响应。

请建议。

I am consuming some service and to consume the service provider has given a certificate.

So I have installed the certificate on LocalMachine and through following code I am attaching the certificate with the web request which i am posting to get response from the web service.

X509Certificate cert = null;
string ResponseXml = string.Empty;

// Represents an X.509 store, which is a physical store
// where certificates are persisted and managed 
X509Store certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);

certStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection results =
    certStore.Certificates.Find(X509FindType.FindBySubjectDistinguishedName,
                                Constants.CertificateName, false);
certStore.Close();

if (results != null && results.Count > 0)
    cert = results[0];
else
{
    ErrorMessage = "Certificate not found";
    return ErrorMessage;
}
webClient.TransportSettings.ClientCertificates.Add(cert);

This works perfectly when i run the code with ASP.net Cassini (ASP.NET Developement Server).

But when i am hosting this code in IIS 7.0 it give forbidden 403 Error as response.

Please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-12-20 23:59:48

您也许应该尝试以下操作:

winhttpcertcfg -g -c LOCAL_MACHINE\MY -s (MyCertificate) -a ASPNET

事实证明,安装证书的用户会自动
授予对私钥的访问权限,我想在您的情况下就是您,所以它可以在开发环境中工作。当 Web 前端出现时,您不再是用户,ASPNET 才是用户。

You should maybe try this:

winhttpcertcfg -g -c LOCAL_MACHINE\MY -s (MyCertificate) -a ASPNET

As it turns out, the user who installs the certificate is automatically
granted access to the private key, I guess then in your case that would be you, so it works in the dev environment. When the web front end comes along, you are no longer the user, ASPNET is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文