使用AddDeveloperSigningCredential部署IdentityServer4解决方案到IIS时出现CryptographicException

4

我有一个基于IdentityServer3和.NET Framework构建的可用Auth服务。我正在构建IdentityServer4和ASP.NET Core的新版本。在开发的早期阶段,我仍在使用AddDeveloperSigningCredential。当我将其部署到我的本地开发框(Windows 10)中的控制台窗口时,它运行良好。当我将其部署到我的本地开发框上的IIS Express时,它在启动时出现以下错误:

UnauthorizedAccessException: Access to the path 'C:\Program Files\IIS Express\tempkey.rsa' is denied.

我并不在意它是否能在IIS Express上运行(因为它在控制台窗口中可以正常运行),但我在这里提供这些信息以防它与我的问题有关。

当我将解决方案部署到运行IIS的远程服务器(Windows Server 2008 R2,IIS 7.5)时,它在启动时会失败,并显示以下错误:

Application startup exception: 

Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Object was not found
   at System.Security.Cryptography.CngKeyLite.GenerateNewExportableKey(String algorithm, Int32 keySize)
   at System.Security.Cryptography.RSAImplementation.RSACng.GetDuplicatedKeyHandle()
   at System.Security.Cryptography.RSAImplementation.RSACng.ExportKeyBlob(Boolean includePrivateParameters)
   at System.Security.Cryptography.RSAImplementation.RSACng.ExportParameters(Boolean includePrivateParameters)
   at Microsoft.Extensions.DependencyInjection.IdentityServerBuilderExtensionsCrypto.AddDeveloperSigningCredential(IIdentityServerBuilder builder, Boolean persistKey, String filename)
   at Orvis.Authorization.Service.Startup.ConfigureServices(IServiceCollection services) in C:\Workspaces\Orvis\orvis-microservices\orvis-authorization\Orvis.Authorization.Service\Startup.cs:line 20

IdentityServer4和特别是AddDeveloperSigningCredential是否能在Windows Server 2008 R2和IIS 7.5上运行,或者我需要一个更新的开发服务器?如果操作系统的年龄不是问题,那么还有什么可能导致这个错误呢?


1
请尽力将问题格式化,特殊情况应放在代码块中。 - Maarten Bodewes
1个回答

2

我已经成功地解决了这个问题。在某个时候,症状发生了变化,我不确定为什么,但我将在此重述我所知道的。

最初,我在我的启动代码中硬编码了.AddDeveloperSigningCertficate调用 - 就像这样:

services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddTestUsers(Config.GetTestUsers());

正如原帖中所指出的那样,这导致了启动时的WindowsCryptographicException。根据一些更新后的IdentityServer4文档,我将其更改为:

            var builder = services.AddIdentityServer()
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddInMemoryApiResources(Config.GetApiResources())
            .AddInMemoryClients(Config.GetClients())
            .AddTestUsers(Config.GetTestUsers());

        if (Environment.IsDevelopment())
        {
            builder.AddDeveloperSigningCredential();
        }
        else
        {
            throw new Exception("Need to configure key material"); 
        }

一旦我确认目标服务器具有正确的环境变量来定义自身为开发环境,我认为这应该产生相同的结果。但是它却产生了访问被拒绝的错误,这与我之前尝试在IIS Express本地部署时看到的错误相同。
鉴于这个错误,我能够授予IIS ApplicationPoolIdentity标识写入权限,以访问c:\ windows \ system32 \ inetsrv,这是它尝试写入tempkey.rsa开发人员凭据文件的位置。 本文提供了ApplicationPoolIdentity的良好概述,并详细介绍了如何为您的应用程序池授予权限。授予该系统目录的权限需要首先获得该目录的所有权。
完成所有这些操作后,在我的开发服务器上使用IIS成功运行我的服务并使用开发人员签名凭据。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接