ASP.NET Core 2.0 - 提供没有扩展名的文件

ASP.NET Core 2.0 - Serving files with no extension

我也在尝试在我的网站上设置 Let's Encrypt。我在网上找到了一堆解决方案,不幸的是 none 对我有用。

我在使用 Apache 2 和 .NET Core 2.0 的 Debian 8.7。

我试过在 .well-known/acme-challenge 文件夹中放置一个 web.config(及其一些变体),但没有成功。我已经尝试了这些链接的解决方案(主要是添加 web.config 和添加一些代码):

https://github.com/ebekker/ACMESharp/issues/15
Set web.config for letsencrypt - Certify with Asp.NET Core and Angular 2 (Javascript-services)

我看过这个,但它是针对已知文件名的,LE 给出了随机文件名,所以我不知道如何实现它:

我知道我得到 URL 错误不是问题,就像我向文件添加扩展名(例如 .t)然后将其添加到 URL 网站是正确返回文件。

这是 acme-challenge 中的 web.config:

<?xml version = "1.0" encoding="UTF-8"?>
 <configuration>
    <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".*" mimeType="text/plain" />
        </staticContent>
        <handlers>
            <clear />
            <add name="StaticFile" path="*" verb="*" type="" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" scriptProcessor="" resourceType="Either" requireAccess="Read" allowPathInfo="false" preCondition="" responseBufferLimit="4194304" />
        </handlers>
     </system.webServer>
 </configuration>

这是整体web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet" arguments=".\my.site.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
  </system.webServer>
</configuration>

这是添加到 Configure() 的代码:

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider("/var/aspnet/miadola/wwwroot/.well-known"),
    RequestPath = new PathString("/var/aspnet/miadola/wwwroot/.well-known"),
    ServeUnknownFileTypes = true // serve extensionless files
});

你的代码看起来不错,除了 1 行。

app.UseStaticFiles(new StaticFileOptions {
    FileProvider = new PhysicalFileProvider("/var/aspnet/miadola/wwwroot/.well-known"),
    RequestPath = new PathString("/.well-known"),
    ServeUnknownFileTypes = true // serve extensionless files
});

发现区别了吗?该行是 RequestPath。该行是您在域后在浏览器中输入的内容。

所以目前你的解决方案点在你去:

www.example.com/var/aspnet/miadola/wwwroot/.well-known


也不需要 web.config 文件,那些是当你在 IIS (windows) 下 运行 时使用的文件。

对我来说,它的工作原理是这样的:

app.UseStaticFiles(); // For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions    //For the '.well-known' folder
     {
          FileProvider = new PhysicalFileProvider(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/.well-known")),
          RequestPath = "/.well-known",
          ServeUnknownFileTypes = true,
     });

并将以下行放入 <system.webServer>

 <staticContent>
      <mimeMap fileExtension=".*" mimeType="text/plain" />
 </staticContent>