WindowsCryptographicException:在 Azure 中发布应用程序时访问被拒绝
WindowsCryptographicException: Access is denied when publishing app in Azure
我有一个非常简单的基于 Aridka OpenIddict 示例的 Web 应用程序,在涉及授权控制器和启动时几乎是它的一个副本 class,但在类型方面存在一些差异返回的资源,但令牌机制与示例完全相同。
该应用程序在我的开发环境中运行良好(数据库在 Azure 中,但该应用程序在我的机器中运行)。但是,我已将它发布到 Azure,只是想看看它是否在托管在云提供商中后仍然有效,它实际上 returns 出现错误并且无法启动。这是堆栈跟踪:
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Access is denied.
at Internal.Cryptography.Pal.StorePal.FromSystemStore(String storeName, StoreLocation storeLocation, OpenFlags openFlags)
at System.Security.Cryptography.X509Certificates.X509Store.Open(OpenFlags flags)
at Microsoft.Extensions.DependencyInjection.OpenIddictServerBuilder.AddDevelopmentEncryptionCertificate(X500DistinguishedName subject)
at Microsoft.Extensions.DependencyInjection.OpenIddictServerBuilder.AddDevelopmentEncryptionCertificate()
at MyApp.Api.Startup.<>c.<ConfigureServices>b__4_4(OpenIddictServerBuilder options) in C:\Users\edm\OneDrive\MyAp\MyApp.Api\Startup.cs:line 75
at Microsoft.Extensions.DependencyInjection.OpenIddictServerExtensions.AddServer(OpenIddictBuilder builder, Action`1 configuration)
at MyApp.Api.Startup.ConfigureServices(IServiceCollection services) in C:\Users\edm\OneDrive\MyApp\MyApp.Api\Startup.cs:line 54
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at MyApp.Api.Program.Main(String[] args) in C:\Users\edm\OneDrive\MyApp\MyApp.Api\Program.cs:line 16
非常感谢任何帮助。我是 OpenId 的新手,所以如果它是一个太基础的问题并且我应该先阅读其他资源,请不要犹豫分享参考资料。
谢谢!
埃德
在门户上添加应用程序设置 WEBSITE_LOAD_USER_PROFILE=1
。
如果不行,试试第二种方法。
将证书添加到您的网络应用程序。
非常感谢所有参考资料。我继续调查。在 [我之前分享的 link][1] 中,OpenIddict 的主要贡献者 Kevin Chalet 明确提到在涉及证书时不要使用示例代码,并建议创建我们自己的。
因此,由于 Pluralsight 中的 Troy Hunt 课程,我学会了如何使用 Powershell 创建自签名证书。有趣的是,Jason 在他的评论中指向了我相同的方向(@Jason,非常感谢,我还不能给你投票,因为我没有足够的 reputation/points,但当我被允许时我会的)。
具体做法如下,供参考:
PS> New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname example.mysite.com
执行此操作时,它将输出指纹和我们证书的主题。
PS> $pwd = ConvertTo-SecureString -String "WHATEVERPASSWORDYOUWANT" -Force -AsPlainText
您定义我们将使用此命令导出证书的 $pwd 变量:
PS> Export-PfxCertificate -cert cert:\localMachine\my\THE_THUMBPRINT_YOU_GOT_EARLIER -FilePath "YOUR_PATH\cert.pfx" -Password $pwd
然后我可以按照 Jason 上面的回答将此证书上传到 App Service,然后在我的代码中我必须通过更改默认值
来更改我的 Startup class
options.AddDevelopmentEncryptionCertificate().AddDevelopmentSigningCertificate();
至
options.AddEncryptionCertificate("THE_THUMBPRINT_YOU_GOT_EARLIER").AddSigningCertificate("THE_THUMBPRINT_YOU_GOT_EARLIER");
最后要做的是将此应用程序设置添加到 Azure 应用程序:
WEBSITE_LOAD_CERTIFICATES 上面的指纹作为值。
注意:为了能够将私有证书 (pfx) 上传到 Azure,您的应用服务计划中至少需要有基本层。
感谢大家
[1]: https://github.com/openiddict/openiddict-core/issues/976
我有一个非常简单的基于 Aridka OpenIddict 示例的 Web 应用程序,在涉及授权控制器和启动时几乎是它的一个副本 class,但在类型方面存在一些差异返回的资源,但令牌机制与示例完全相同。
该应用程序在我的开发环境中运行良好(数据库在 Azure 中,但该应用程序在我的机器中运行)。但是,我已将它发布到 Azure,只是想看看它是否在托管在云提供商中后仍然有效,它实际上 returns 出现错误并且无法启动。这是堆栈跟踪:
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Access is denied.
at Internal.Cryptography.Pal.StorePal.FromSystemStore(String storeName, StoreLocation storeLocation, OpenFlags openFlags)
at System.Security.Cryptography.X509Certificates.X509Store.Open(OpenFlags flags)
at Microsoft.Extensions.DependencyInjection.OpenIddictServerBuilder.AddDevelopmentEncryptionCertificate(X500DistinguishedName subject)
at Microsoft.Extensions.DependencyInjection.OpenIddictServerBuilder.AddDevelopmentEncryptionCertificate()
at MyApp.Api.Startup.<>c.<ConfigureServices>b__4_4(OpenIddictServerBuilder options) in C:\Users\edm\OneDrive\MyAp\MyApp.Api\Startup.cs:line 75
at Microsoft.Extensions.DependencyInjection.OpenIddictServerExtensions.AddServer(OpenIddictBuilder builder, Action`1 configuration)
at MyApp.Api.Startup.ConfigureServices(IServiceCollection services) in C:\Users\edm\OneDrive\MyApp\MyApp.Api\Startup.cs:line 54
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at MyApp.Api.Program.Main(String[] args) in C:\Users\edm\OneDrive\MyApp\MyApp.Api\Program.cs:line 16
非常感谢任何帮助。我是 OpenId 的新手,所以如果它是一个太基础的问题并且我应该先阅读其他资源,请不要犹豫分享参考资料。
谢谢!
埃德
在门户上添加应用程序设置
WEBSITE_LOAD_USER_PROFILE=1
。如果不行,试试第二种方法。
将证书添加到您的网络应用程序。
非常感谢所有参考资料。我继续调查。在 [我之前分享的 link][1] 中,OpenIddict 的主要贡献者 Kevin Chalet 明确提到在涉及证书时不要使用示例代码,并建议创建我们自己的。
因此,由于 Pluralsight 中的 Troy Hunt 课程,我学会了如何使用 Powershell 创建自签名证书。有趣的是,Jason 在他的评论中指向了我相同的方向(@Jason,非常感谢,我还不能给你投票,因为我没有足够的 reputation/points,但当我被允许时我会的)。
具体做法如下,供参考:
PS> New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname example.mysite.com
执行此操作时,它将输出指纹和我们证书的主题。
PS> $pwd = ConvertTo-SecureString -String "WHATEVERPASSWORDYOUWANT" -Force -AsPlainText
您定义我们将使用此命令导出证书的 $pwd 变量:
PS> Export-PfxCertificate -cert cert:\localMachine\my\THE_THUMBPRINT_YOU_GOT_EARLIER -FilePath "YOUR_PATH\cert.pfx" -Password $pwd
然后我可以按照 Jason 上面的回答将此证书上传到 App Service,然后在我的代码中我必须通过更改默认值
来更改我的 Startup classoptions.AddDevelopmentEncryptionCertificate().AddDevelopmentSigningCertificate();
至
options.AddEncryptionCertificate("THE_THUMBPRINT_YOU_GOT_EARLIER").AddSigningCertificate("THE_THUMBPRINT_YOU_GOT_EARLIER");
最后要做的是将此应用程序设置添加到 Azure 应用程序:
WEBSITE_LOAD_CERTIFICATES 上面的指纹作为值。
注意:为了能够将私有证书 (pfx) 上传到 Azure,您的应用服务计划中至少需要有基本层。
感谢大家 [1]: https://github.com/openiddict/openiddict-core/issues/976