【发布时间】:2018-10-22 13:06:09
【问题描述】:
我正在尝试使用 Azure 的 Web 应用服务托管 ASP.NET Core 2 Web 应用程序。该网络应用程序在我的开发机器(Windows 10)上运行良好。当我发布文件时,它也适用于 Ubuntu 17.10 虚拟机。但是,当我将 Web 应用程序从 Visual Studio 发布到 Azure 时,我收到以下错误消息:
WindowsCryptographicException: The system cannot find the file specified
System.Security.Cryptography.CngKey.Open(string keyName, CngProvider provider, CngKeyOpenOptions openOptions)
System.Security.Cryptography.CngKey.Open(string keyName, CngProvider provider)
Internal.Cryptography.Pal.CertificatePal.GetPrivateKey<T>(Func<CspParameters, T> createCsp, Func<CngKey, T> createCng)
Internal.Cryptography.Pal.CertificatePal.GetRSAPrivateKey()
Internal.Cryptography.Pal.CertificateExtensionsCommon.GetPrivateKey<T>(X509Certificate2 certificate, Predicate<X509Certificate2> matchesConstraints)
...
Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter+<>c__DisplayClass3_0.<Configure>b__0(IApplicationBuilder app)
Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
.NET Core 4.6.26212.01 X86 v4.0.0.0 | Microsoft.AspNetCore.Hosting version 2.0.2-rtm-10011 | Microsoft Windows 10.0.14393
我正在项目目录中存储一个密钥文件并尝试从中创建凭据。
经过搜索,我找到了以下指向类似解决方案的网站: https://github.com/dotnet/corefx/issues/11042、https://github.com/IdentityServer/IdentityServer4/issues/1432、Certificate not found when deploying IdentityServer 4 to Azure VM 和 CryptographicException was unhandled: System cannot find the specified file
这些建议我需要修改应用程序池的设置并将“加载用户配置文件”设置为true并正确设置文件的权限。但是,我不确定如何使用Azure Portal 执行此操作。
此外,除了修改服务器上的文件之外,是否有可以将代码添加到 Program.cs 或 Startup.cs 的解决方案(摘录如下)?
Program.cs (referenced link)
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseSetting("detailedErrors", "true")
.UseIISIntegration()
.UseStartup<Startup>()
.CaptureStartupErrors(true)
.Build();
}
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseMvc();
}
}
提前致谢。
更新 #1
应用部署正确。我只是在执行以下代码时遇到错误。
private static RSA GetPrivateKey(byte[] p12) {
var certificate = new X509Certificate2(p12, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
var rsa = certificate.GetRSAPrivateKey();
return rsa;
}
这是我的项目 .csproj 文件:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GDataDB" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.3" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
</ItemGroup>
<ItemGroup>
<Folder Include="Pages\" />
<Folder Include="Properties\PublishProfiles\" />
</ItemGroup>
<ItemGroup>
<None Update="total-method-203123-88f147135d99.p12">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
更新 #2
这是一个演示错误的最小 repo:https://github.com/jonliew/testdatawebapp
我首先使用 Visual Studio 中的 FolderProfile 将 GDataDB 发布到 nuget 包。然后,我将包添加到 DataWebApp 项目。最后,我发布 DataWebApp 并得到上述行为。
【问题讨论】:
-
我尝试设置 X509KeyStorageFlags 无济于事。其他解决方案建议我需要加载我不确定的用户配置文件。
标签: asp.net-core azure-web-app-service