相关疑难解决方法(0)

如何让ASP.NET访问证书存储区中的证书中的私钥?

我有一个ASP.NET应用程序访问证书存储区中的证书中的私钥.在Windows Server 2003上,我能够使用winhttpcertcfg.exe来授予对NETWORK SERVICE帐户的私钥访问权限.如何授予访问IIS 7.5网站中Windows Server 2008 R2上的证书存储(本地计算机\个人)中的证书中的私钥的权限?

我尝试使用证书MMC(Server 2008 R2)为"Everyone","IIS AppPool\DefaultAppPool","IIS_IUSRS"以及我能找到的每个其他安全帐户提供完全信任访问权限.但是,下面的代码演示了代码无法访问使用私钥导入的证书的私钥.每次访问私钥属性时,代码都会抛出并抛出错误.

Default.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.Security.Cryptography.X509Certificates" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="repeater1" runat="server">
            <HeaderTemplate>
                <table>
                    <tr>
                        <td>
                            Cert
                        </td>
                        <td>
                            Public Key
                        </td>
                        <td>
                            Private Key
                        </td>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                    <%#((X509Certificate2)Container.DataItem).GetNameInfo(X509NameType.SimpleName, false) %>
                    </td>
                    <td>
                    <%#((X509Certificate2)Container.DataItem).HasPublicKeyAccess() %>
                    </td>
                    <td>
                    <%#((X509Certificate2)Container.DataItem).HasPrivateKeyAccess() %>
                    </td>
                </tr> …

asp.net certificate winhttp iis-7.5

108
推荐指数
5
解决办法
15万
查看次数

PrivateKey 抛出了 System.Security.Cryptography.CryptographicException 类型的异常

我正在尝试使用以下代码使用自签名证书:

X509Certificate2 cert = ToCertificate("CN=localhost");


public static X509Certificate2 ToCertificate(this string subjectName,
                                                StoreName name = StoreName.My,
                                                StoreLocation location = StoreLocation.LocalMachine
                                                )
    {
        X509Store store = new X509Store(name, location);

        store.Open(OpenFlags.ReadOnly);

        try
        {
            var cert = store.Certificates.OfType<X509Certificate2>().FirstOrDefault(c => c.Subject.Equals(subjectName, StringComparison.OrdinalIgnoreCase));

            return cert != null ? new X509Certificate2(cert) : null;
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            store.Certificates.OfType<X509Certificate2>().ToList().ForEach(c => c.Reset());
            store.Close();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我收到以下异常:

PrivateKey = 'cert.PrivateKey' threw an exception of type 'System.Security.Cryptography.CryptographicException'
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我试过这个修复这个修复

但是还是有问题!

c# digital-certificate x509certificate2

5
推荐指数
2
解决办法
1万
查看次数