简体   繁体   English

WindowsCryptographicException:在 Azure 中发布应用程序时访问被拒绝

[英]WindowsCryptographicException: Access is denied when publishing app in Azure

I have a very simple Web App based on the Aridka OpenIddict sample, pretty much a copy of it when it comes to the Authorization controller and the startup class, with some differences with regards to the type of resource that is returned, but the token mechanism is exactly as per the sample.我有一个基于 Aridka OpenIddict 示例的非常简单的 Web 应用程序,当涉及到授权 controller 和启动 class 的机制时,它几乎是它的副本,但关于资源类型的差异,完全按照样本。

The App works well in my dev environment (the db is in Azure, but the app runs in my machine).该应用程序在我的开发环境中运行良好(数据库位于 Azure,但该应用程序在我的机器上运行)。 However, I have published it to Azure just to see if it still worked once hosted in the cloud provider and it actually returns an error and does not start.但是,我已将其发布到 Azure 只是为了查看它是否仍然可以在云提供商中托管并且它实际上返回错误并且无法启动。 This is the stack trace:这是堆栈跟踪:

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\1\1\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\1\1\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\1\1\MyApp.Api\Program.cs:line 16

I really appreciate any help.我真的很感激任何帮助。 I am new to OpenId so if it's too basic a question and there are other resources I should read first, do not hesitate to share references.我是 OpenId 的新手,所以如果这个问题太基本并且我应该先阅读其他资源,请不要犹豫分享参考资料。

Thank you!谢谢!

Ed埃德

  1. Add application setting WEBSITE_LOAD_USER_PROFILE=1 on portal.在门户上添加应用程序设置WEBSITE_LOAD_USER_PROFILE=1

    If not works, try the second method.如果不起作用,请尝试第二种方法。

  2. Add CERTIFICATES to your web app.将证书添加到您的 web 应用程序。

    Upload certificate to App Service 将证书上传到应用服务

Thanks a lot for all the references.非常感谢所有的参考。 I have continued to investigate.我继续调查。 In [the link I shared previously][1], Kevin Chalet, the lead contributor of OpenIddict made an explicit mention to not use the sample code when it comes to certificates and suggested to create our own.在 [我之前分享的链接][1] 中,OpenIddict 的主要贡献者 Kevin Chalet 明确提到在证书方面不要使用示例代码,并建议创建我们自己的。

So I learned how to create self-signed certificates with Powershell thanks to a Troy Hunt course in Pluralsight.因此,感谢 Pluralsight 中的 Troy Hunt 课程,我学会了如何使用 Powershell 创建自签名证书。 Funny that Jason was pointing me in the same direction in his comment (@Jason, thanks a lot, I cannot upvote you yet as I don't have enough reputation/points, but I will when I am allowed).有趣的是,杰森在他的评论中向我指出了相同的方向(@Jason,非常感谢,我还不能投票给你,因为我没有足够的声誉/积分,但当我被允许时我会)。

Here is how to do it, for reference:以下是操作方法,供参考:

PS> New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname example.mysite.com

When executing this it will output the thumprint and the subject of our certificate.执行此操作时,它将 output 指纹和我们证书的主题。

PS> $pwd = ConvertTo-SecureString -String "WHATEVERPASSWORDYOUWANT" -Force -AsPlainText

You define the $pwd variable that we will use to export the certificate with this command:您使用以下命令定义我们将用于导出证书的 $pwd 变量:

PS> Export-PfxCertificate -cert cert:\localMachine\my\THE_THUMBPRINT_YOU_GOT_EARLIER -FilePath "YOUR_PATH\cert.pfx" -Password $pwd

Then I could upload this certificate to App Service as Jason replied above, and then in my code I had to change my Startup class by changing the default然后我可以按照上面 Jason 的回答将此证书上传到 App Service,然后在我的代码中我必须通过更改默认值来更改我的 Startup class

options.AddDevelopmentEncryptionCertificate().AddDevelopmentSigningCertificate();

to

options.AddEncryptionCertificate("THE_THUMBPRINT_YOU_GOT_EARLIER").AddSigningCertificate("THE_THUMBPRINT_YOU_GOT_EARLIER");

And the last thing to do was to add this application setting to the Azure App:最后要做的是将此应用程序设置添加到 Azure 应用程序中:

WEBSITE_LOAD_CERTIFICATES with the thumbprint from above as the value. WEBSITE_LOAD_CERTIFICATES 上面的指纹作为值。

Note : in order to be able to upload the private certificate (pfx) into Azure you need to have at a minimum the Basic tier in your app service plan.注意:为了能够将私有证书 (pfx) 上传到 Azure,您的应用服务计划中至少需要具有基本层。

Thanks to all [1]: https://github.com/openiddict/openiddict-core/issues/976感谢所有 [1]: https://github.com/openiddict/openiddict-core/issues/976

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM