【问题标题】:WindowsCryptographicException running ASP.NET Core 2 web app in Azure Web App Service在 Azure Web 应用服务中运行 ASP.NET Core 2 Web 应用的 WindowsCryptographicException
【发布时间】: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/11042https://github.com/IdentityServer/IdentityServer4/issues/1432Certificate not found when deploying IdentityServer 4 to Azure VMCryptographicException 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


【解决方案1】:

问题很可能是您的目标是 .NET Core 2.0.8,它尚未完全部署在应用服务上。公告见here

要验证这是否确实是问题所在,请尝试部署到 美国西部 2 区域(不是美国西部)的测试 Web 应用,该应用确实具有发布。你不应该在那里得到错误。

【讨论】:

  • 我相信我的目标是 .NET Core 2.0.7,因为那是我的项目正在使用的 Microsoft.AspNetCore.All 的版本。我尝试在新资源组中部署到美国西部 2 区域,但遇到了同样的错误。该应用程序正确部署;只有在执行以下代码时才会遇到错误: private static RSA GetPrivateKey(byte[] p12) { var certificate = new X509Certificate2(p12, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable); var rsa = 证书.GetRSAPrivateKey();返回 rsa; }
  • 这很奇怪。作为测试,如果您改为以 2.0.8 为目标,它是否适用于美国西部 2?
  • 在您回复后,我对上述评论进行了编辑。如果将 Microsoft.AspNetCore.All nuget 包更新到 2.0.8 以 .NET Core 2.0.8 为目标,那么将 2.0.8 部署到 West US 2 将无济于事。我在上面的评论中包含了代码,在 Update #1 中包含了我的 .csproj。
  • 能否请share a minimal repo 说明问题?它将帮助我们进行调查。谢谢。
  • 这里是一个最小 repo 的链接:github.com/jonliew/testdatawebapp我在原始帖子中的更新描述了我是如何构建它的。
猜你喜欢
  • 2019-06-28
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2017-05-10
  • 1970-01-01
  • 2023-02-01
  • 2018-09-22
相关资源
最近更新 更多