首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何让ASP.NET访问证书存储区中的证书中的私钥?

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

Stack Overflow用户
提问于 2018-03-08 02:10:31
回答 2查看 0关注 0票数 0

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

我已经尝试给“Everyone”,“IIS AppPool \ DefaultAppPool”,“IIS_IUSRS”以及使用证书MMC(Server 2008 R2)找到的其他安全帐户给予完全信任访问权限。但是,下面的代码表明代码无权访问使用私钥导入的证书的私钥。每次访问私钥属性时,代码会抛出并出错。

default.aspx

代码语言:txt
复制
<%@ 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>
            </ItemTemplate>
            <FooterTemplate>
                </table></FooterTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

代码语言:txt
复制
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Web.UI;
public partial class _Default : Page 
{
    public X509Certificate2Collection Certificates;
    protected void Page_Load(object sender, EventArgs e)
    {
        // Local Computer\Personal
        var store = new X509Store(StoreLocation.LocalMachine);
        // create and open store for read-only access
        store.Open(OpenFlags.ReadOnly);
        Certificates = store.Certificates;
        repeater1.DataSource = Certificates;
        repeater1.DataBind();
    }
}
public static class Extensions
{
    public static string HasPublicKeyAccess(this X509Certificate2 cert)
    {
        try
        {
            AsymmetricAlgorithm algorithm = cert.PublicKey.Key;
        }
        catch (Exception ex)
        {
            return "No";
        }
        return "Yes";
    }
    public static string HasPrivateKeyAccess(this X509Certificate2 cert)
    {
        try
        {
            string algorithm = cert.PrivateKey.KeyExchangeAlgorithm;
        }
        catch (Exception ex)
        {
            return "No";
        }
        return "Yes";
    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-03-08 10:53:46

  1. 创建/购买证书。确保它有一个私钥。
  2. 将证书导入“本地计算机”帐户。最好使用证书MMC。确保选中“允许私钥导出”
  3. 基于此,IIS 7.5应用程序池的身份使用以下之一。
代码语言:txt
复制
- IIS 7.5 Website is running under ApplicationPoolIdentity. Open MMC => Add Certificates (Local computer) snap-in => Certificates (Local Computer) => Personal => Certificates => Right click the certificate of interest => All tasks => Manage private key => Add `IIS AppPool\AppPoolName` and grant it `Full control`. Replace "_AppPoolName_" with the name of your application pool (sometimes `IIS_IUSRS`)
- IIS 7.5 Website is running under NETWORK SERVICE. Using Certificates MMC, added "NETWORK SERVICE" to Full Trust on certificate in "Local Computer\Personal".
- IIS 7.5 Website is running under "MyIISUser" local computer user account. Using Certificates MMC, added "MyIISUser" (a new local computer user account) to Full Trust on certificate in "Local Computer\Personal".

请注意,如果您使用的是域名,则默认情况下,域名将在“来自地理位置”框中被选中。确保将其更改为“本地计算机”。将位置更改为“本地计算机”以查看应用程序池标识。

票数 0
EN

Stack Overflow用户

发布于 2018-03-08 11:48:11

关于通过MMC,Certs,Select Cert授权,右键单击,所有任务,“管理私钥”

管理私钥只在个人的菜单列表中...所以如果你已经将你的证书放在可信任的人等等中,那么你的运气不好。

我们找到了解决这个问题的方法。将证书拖放到个人,执行管理私人密钥的事情来授予权限。请记住设置为使用对象类型的内置插件并使用本地计算机而不是域。我们授予DefaultAppPool用户权利,并将其留在那里。

完成之后,将证书拖放回原来的位置。普雷斯托。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档