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

时间:2018-05-18 14:28:02

标签: c# asp.net-core .net-core lets-encrypt kestrel-http-server

我正在尝试在我的网站上设置Let's Encrypt。我在网上找到了很多解决方案,遗憾的是没有一个解决方案适合我。

我使用的是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提供随机文件名,所以我不知道如何实现它: asp.net core - How to serve static file with no extension

我知道,如果我将URL错误添加到文件中,然后将其添加到网站正确返回文件的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
});

2 个答案:

答案 0 :(得分:4)

你的代码看起来不错,除了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)下运行的文件。

答案 1 :(得分:0)

对我来说,它的工作原理如下:

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>
相关问题