主持 .well-known/openid-configuration 的正确方法是什么?

What is the correct way to host .well-known/openid-configuration?

我有一个基于 ASP.NET Core 5 的 Blazor 服务器项目。我想托管我自己的 openid 配置发现文件。由于此文件是在 运行 设置 OIDC 工作流程时提供的,我想验证托管此文件的正确方法是什么。到目前为止,我已经尝试了以下方法,但只有选项 2 有效。

  1. 使用 wwwroot/.well-known

这涉及在我的 Blazor 服务器项目的 wwwroot 文件夹中静态托管 openid-configuration 文件。

在此之后,如果我 运行 他项目并尝试使用 localhost:44382/ 访问文件。well-known/openid-configuration,文件未提供。

  1. 使用控制器

为此,我刚刚向我的 blazor 项目添加了一个简单的控制器,并指定了 .well-known/openid-configuration 作为我的匿名控制器 HTTPGET 操作的路由。

public class OidcConfigurationController : Controller
{      

    [HttpGet(".well-known/openid-configuration")]
    public JsonResult OpenIdConfiguration()
    {
        return Json(new Storage.Storables.Security.OIDC.Configuration());
    }   
}

现在,如果我 运行 使用选项 2 的项目并尝试到达 localhost:44382/.well-known/openid-configuration, 配置 JSON 将得到正确服务.

选项 2 是使用 ASP.NET Core 和 Blazor 服务器项目提供 OpenId-Configuration 的正确方法吗?如果我发布服务器(例如发布到 Azure)是否会导致任何问题

第一种方法不起作用的原因是您没有按照静态文件扩展名假设的方式提供静态文件。您缺少文件结尾,否则请求将无法识别为文件。

也就是说,您可以编写自己的中间件。给文件一个正确的结尾,例如 .json。如果请求资源 /.well-known/openid-configuration/,则将请求的路径更改为 /.well-known/openid-configuration.json 并让静态文件扩展名处理其余部分。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
            ...
          

            app.Use(async (context, next) =>
            {
                if (context.Request.Path == "/.well-known/openid-configuration")
                {
                    context.Request.Path = "/.well-known/openid-configuration.json";
                }
           
                await next();
            });

            app.UseStaticFiles();

            ...
}

有关编写中间件的更多信息,请查看文档 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write

但是,您 运行 遇到了问题 - 我猜 - 因为大部分文档是根据您的开放 ID 连接服务器(如 IdentityServer)的配置动态生成的。所以,也许静态文件周围有什么?