我正在尝试使用Azure的Web App Service托管ASP.NET Core 2 Web应用程序。 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>
Udpate#2
这是一个演示错误的最小回购:https://github.com/jonliew/testdatawebapp
我首先使用Visual Studio中的FolderProfile将GDataDB发布到nuget包。然后,我将包添加到DataWebApp项目。最后,我发布了DataWebApp并获得了上述行为。
答案 0 :(得分:0)
最有可能的问题是,您的目标是.NET Core 2.0.8,它尚未完全部署在App Service上。有关公告,请参阅here。
要验证这是否确实是问题,请尝试部署到 West US 2 区域(不是 West US)的测试Web应用程序,该区域确实具有发布。你不应该在那里得到错误。