如何在microsoft azure中将.pem托管在/.well-known下? ASP.NET Core MVC

问题描述 投票:0回答:1

目标:我正在尝试在 Azure 中的

https://example.com/.well-known/XXX.pem
下托管公共证书 (.pem)。我正在使用 ASP.NET Core MVC 项目。

我已将证书添加到项目文件夹中

wwwroot/.well-known/xxxx.pem

我能够验证该文件是否存在于构建输出(

Bin/Release
文件夹)中。这与项目构建设置密切相关:

Build Action: Content
Copy To output directory: Copy Always

但是,一旦部署完成,就无法通过 Azure Web 应用程序托管的实际 Web 进行访问。它返回 404 文件未找到。

我已经通过跳转到 kudu 控制台来验证,该文件确实存在于

wwwroot/.well-known
文件夹中。我怀疑 Azure 阻止了内容的提供。

我做错了什么?

asp.net-core-mvc azure-web-app-service ssl-certificate
1个回答
0
投票

我尝试使用以下代码在 Azure 应用服务的 .well-known 文件夹中托管 .pem 证书。

我将下面的代码添加到我的Program.cs类中,如下所示:

app.MapControllerRoute(
               name: "well-known",
               pattern: ".well-known/{*fileName}",
               defaults: new { controller = "Certificate", action = "GetCertificate" });

Program.cs

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
               name: "well-known",
               pattern: ".well-known/{*fileName}",
               defaults: new { controller = "Certificate", action = "GetCertificate" });

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();

CertificateController.cs

using Microsoft.AspNetCore.Mvc;
namespace certapp.Controllers
{
    public class CertificateController : Controller
    {
        [Route(".well-known/{fileName}")]
        public IActionResult GetCertificate(string fileName)
        {
            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", ".well-known", fileName);
            if (System.IO.File.Exists(filePath))
            {
                var fileContent = System.IO.File.ReadAllText(filePath);
                return Content(fileContent, "text/plain");
            }
            return NotFound();
        }
    }
}

我将Certificate.pem添加到.wellknown文件夹、wwwroot文件夹、根目录下,如下图:

.众所周知的文件夹 :

enter image description here

本地输出

enter image description here

我成功将应用程序发布到Azure App Service,如下所示:

enter image description here

Azure Web 应用程序输出enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.