3

I'm trying too set up Let's Encrypt on my site. I have found a bunch of solutions online, unfortunately none have worked for me.

I'm on Debian 8.7 with Apache 2 and .NET Core 2.0.

I've tried placing a web.config (and a few variations of it) in the .well-known/acme-challenge folder to no luck. I've triedthe solutions at these links (mainly adding a web.config and adding some code):

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

I have seen this but it's for a known file name, LE gives random file names so I don't know how to implement it: asp.net core - How to serve static file with no extension

I know it's not an issue with me getting the URL wrong as if I add an extension (for example .t) to the file and then add that to the URL the site is correctly returning the file.

Here's the web.config in acme-challenge:

<?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>

Here's the overall 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>

Here's the code added to 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
});
johnny 5
  • 19,893
  • 50
  • 121
  • 195
Goigle
  • 138
  • 13

2 Answers2

9

You code looks good, except for 1 line.

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

Spotted the difference? The line is the RequestPath. That line is what you enter in the browser after the domain.

So currently your solution points works when you go to:

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


Also there is no need for the web.config files, those are for when you are running under IIS (windows).

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
  • Yup, that fixed it. I did have that initially but I had the `/.well-known` in both PathString() and PhysicalFileProvider() and then I got an error so I did the opposite... Thank you so much! (I'll mark your answer as accepted when it lets me) Ironically, LE is now undergoing maintenance so I still can't get the cert for a bit :') – Goigle May 18 '18 at 14:39
  • ServeUnknownFileTypes is crucial for Squirrel .Net updates since their RELEASES file is extensionless. Nice work glad I found this, hopefully others will find this too. – Jeff Barnard Mar 29 '22 at 13:45
7

For me it worked by something like this:

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,
     });

and also putting following lines inside <system.webServer>

 <staticContent>
      <mimeMap fileExtension=".*" mimeType="text/plain" />
 </staticContent>
Mojtaba Karimi
  • 426
  • 7
  • 10