我有一个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>
</ItemTemplate>
<FooterTemplate>
</table></FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Default.aspx.cs ``
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";
}
}
``
5 回答
创建/购买证书 . 确保它有一个私钥 .
将证书导入"Local Computer"帐户 . 最好使用证书MMC . Make sure to check "Allow private key to be exported"
基于此,IIS 7.5应用程序池的标识使用以下之一 .
IIS 7.5网站在ApplicationPoolIdentity下运行 . 打开MMC =>添加证书(本地计算机)管理单元=>证书(本地计算机)=>个人=>证书=>右键单击感兴趣的证书=>所有任务=>管理私钥=>添加
IIS AppPool\AppPoolName
并授予它Full control
. 将“AppPoolName”替换为应用程序池的名称(有时IIS_IUSRS
)IIS 7.5网站正在NETWORK SERVICE下运行 . 使用证书MMC,在"Local Computer\Personal"的证书上将"NETWORK SERVICE"添加到完全信任 .
IIS 7.5网站在"MyIISUser"本地计算机用户帐户下运行 . 使用证书MMC,在"Local Computer\Personal"中的证书上将"MyIISUser"(新的本地计算机用户帐户)添加到完全信任 .
基于@Phil Hale的更新评论:
请注意,如果您在某个域中,默认情况下会在“从位置框”中选择您的域名 . 务必将其更改为“本地计算机” . 将位置更改为“本地计算机”以查看应用程序池标识 .
关于通过MMC,Certs,Select Cert,右键单击,所有任务,“管理私钥”授予权限的注意事项
管理私钥只在个人菜单列表中...所以,如果你把你的证书放在受信任的人等等,那你运气不好 .
我们找到了解决这个问题的方法,对我们有用 . 将证书拖放到Personal,执行Manage Private Keys事务以授予权限 . 请记住设置为使用对象类型内置函数并使用本地计算机而不是域 . 我们授予DefaultAppPool用户权限并将其留在那里 .
完成后,将证书拖放回原来的位置 . 普雷斯托 .
如果您尝试从IIS中的.pfx文件加载证书,解决方案可能就像为
Application Pool
启用此选项一样简单 .右键单击App Pool并选择
Advanced Settings
.然后启用
Load User Profile
我想知道如何在Powershell中执行此操作,有人问:
对我来说,只需重新导入证书并选中“允许导出私钥” .
我想这是必要的,但它确实让我感到紧张,因为它是访问此证书的第三方应用程序 .