tha*_*mes 108 asp.net certificate winhttp iis-7.5
我有一个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>
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"; } }
<%@ 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>
tha*_*mes 187
基于此,IIS 7.5应用程序池的标识使用以下之一.
IIS AppPool\AppPoolName
并授予它Full control
.将" AppPoolName " 替换为应用程序池的名称(有时IIS_IUSRS
)基于@Phil Hale的更新评论:
请注意,如果您在某个域中,默认情况下会在"从位置框"中选择您的域名.务必将其更改为"本地计算机".将位置更改为"本地计算机"以查看应用程序池标识.
小智 42
关于通过MMC,Certs,Select Cert,右键单击,所有任务,"管理私钥"授予权限的注意事项
管理私人密钥仅在个人菜单列表中...因此,如果您已将您的证书放在受信任的人等中,那么您将失去运气.
我们找到了解决这个问题的方法,对我们有用.将证书拖放到Personal,执行Manage Private Keys事务以授予权限.请记住设置为使用对象类型内置函数并使用本地计算机而不是域.我们授予了DefaultAppPool用户的权限,并将其保留在那里.
完成后,将证书拖放回原来的位置.普雷斯托.
Sim*_*ver 12
如果您尝试从IIS中的.pfx文件加载证书,解决方案可能就像启用此选项一样简单Application Pool
.
右键单击App Pool并选择Advanced Settings
.
然后启用 Load User Profile
我想出了如何在Powershell中执行此操作,有人问过:
$keyname=(((gci cert:\LocalMachine\my | ? {$_.thumbprint -like $thumbprint}).PrivateKey).CspKeyContainerInfo).UniqueKeyContainerName
$keypath = $env:ProgramData + “\Microsoft\Crypto\RSA\MachineKeys\”
$fullpath=$keypath+$keyname
$Acl = Get-Acl $fullpath
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS AppPool\$iisAppPoolName", "Read", "Allow")
$Acl.SetAccessRule($Ar)
Set-Acl $fullpath $Acl
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
150752 次 |
最近记录: |