ASP.NET 可以在没有文件系统权限的情况下从证书存储中获取 X509Certificate 吗?

StackOverflow https://stackoverflow.com//questions/20007824

证书应该出现在哪里 Trusted Root Certification Authorities 节点在 证书管理器MSC 是否保留以便 IIS Web 应用程序可以获取它并用它签署 SAML 断言?有没有一种方法可以直接从证书“配置单元”中提取证书,而不需要文件系统权限?或者是否需要将证书导出到IIS7内置用户有访问权限的文件夹中?

X509Certificate2.Import() 方法的第一个参数是 fileName.

如果我导出证书并将文件放入 Visual Studio 2012 项目文件夹层次结构中,并提供指向该证书的完全限定路径 进口() 方法,证书导入成功,但前提是应用程序在 Visual Studio 中运行 内置 服务器,如果它在本地 IIS Web 服务器中运行,则不是。

我尝试过使用 友好名称X509KeyStorageFlags.MachineKeySet 但这没有用。

编辑:当使用内置的 Visual Studio 服务器而不是 Windows 7 中的本地 IIS7 服务器时,此方法有效:

            certStore = New X509Store(StoreLocation.CurrentUser)
            certStore.Open(OpenFlags.ReadOnly)
            Dim thumbprint As String
            thumbprint = ConfigurationManager.AppSettings("thumb").ToString
            certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, False)
            certStore.Close()
            cert = certCollection(0)

所以我需要了解如何在 Windows 7 中让默认应用程序池访问此证书。

有帮助吗?

解决方案

您不需要“导入”,只需创建一个实例。正式地,您打开一个密钥存储并循环访问证书。是的,您不需要任何文件系统权限,但是,要访问私钥,您的应用程序池身份必须拥有该密钥的权限,您可以在 mmc 控制台的证书管理单元中设置权限。

编辑:访问证书的代码类似于:

var store = new X509Store( name, location );
store.Open( OpenFlags.ReadOnly );

foreach ( var cert in store.Certificates )
   ... loop and match, by thumbprint, friendly name or whatever else
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top