代码之家  ›  专栏  ›  技术社区  ›  Nicole Demera

ASP。NET Core 6如何使用。设置https的pem链文件

  •  0
  • Nicole Demera  · 技术社区  · 3 年前

    我有一个用asp编写的应用程序。net core 6,我想我已经具备了设置https所需的功能。我有一本书。pem链锉和一个。我可以使用pem文件,而且我还有一个。密钥文件,该文件是在我生成。csr文件。

    我查阅了所有我能找到的关于这个的文件。

    这似乎是最相关的一个,但没有提到任何这些文件类型,确切地说,它提到了使用。使用HTTPS(<pem文件名>,<密钥文件名>)但这让

    Unhandled exception. System.NotSupportedException: The server mode SSL must use a certificate with the associated private key.
       at System.Net.Security.SslStreamCertificateContext.Create(X509Certificate2 target, X509Certificate2Collection additionalCertificates, Boolean offline, SslCertificateTrust trust)
       at Microsoft.AspNetCore.Server.Kestrel.Https.Internal.HttpsConnectionMiddleware..ctor(ConnectionDelegate next, HttpsConnectionAdapterOptions options, ILoggerFactory loggerFactory)
       at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.<>c__DisplayClass12_0.<UseHttps>b__0(ConnectionDelegate next)
       at Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions.Build()
       at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass30_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()
    --- End of stack trace from previous location ---
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Server.Kestrel.Core.AnyIPListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IEnumerable`1 listenOptions, AddressBindContext context, CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
       at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
       at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
       at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
       at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
       at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
       at Microsoft.AspNetCore.Builder.WebApplication.Run(String url)
       at Program.<Main>$(String[] args) in C:\Users\Nicole\source\repos\TestWebAppRazor\TestWebAppRazor\Program.cs:line 43
    

    这是我的节目。反恐精英

    using Microsoft.AspNetCore.Authentication.Certificate;
    using Microsoft.AspNetCore.Server.Kestrel.Https;
    using Microsoft.AspNetCore.Server.Kestrel.Core;
    using Microsoft.AspNetCore.Hosting;
    
    var builder = WebApplication.CreateBuilder(args);
    // Add services to the container.
    builder.Services.AddRazorPages();
    builder.WebHost.ConfigureKestrel(serverOptions =>
    {
        serverOptions.ConfigureEndpointDefaults(listenOptions =>
        {
            listenOptions.UseHttps("mywebsite_com.pem", "mywebsite.com.key");
        });
    });
    builder.Services.Configure<KestrelServerOptions>(options =>
    {
        options.ConfigureHttpsDefaults(options =>
            options.ClientCertificateMode = ClientCertificateMode.RequireCertificate);
    });
    builder.Services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme).AddCertificate();
    builder.Host.UseSystemd();
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    
    app.UseRouting();
    
    app.UseAuthorization();
    app.UseAuthentication();
    
    app.MapRazorPages();
    
    app.Run();
    

    我知道我做错了,但我很难破译文件。

    如果您能为我提供任何指导或指导,帮助我查找相关文档,我将不胜感激。

    0 回复  |  直到 3 年前
    推荐文章