Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static files outside the wwwroot for .netcore app

I'm using https://github.com/ebekker/ACMESharp for my SSL at my @home web-server (it's free! :O). It was pretty manual, but noticed on the wiki it mentioned another project at https://github.com/Lone-Coder/letsencrypt-win-simple which was a GUI for the automation of applying for, downloading, and installing of your SSL cert to your web-server.

The method the GUI uses to validate the domain is yours, is by created a randomly named file with a random string of text within [webroot]/.well-known/[randomFile] w/o an extension. With the .dotnetcore application running on this [webroot], I am unable to serve the file, even after following the instructions for changing "Handler Mappings" under IIS.

It seems like I can serve files by navigating directly to them at [webRoot]/wwwroot/[whatever] - so why can't I in [webroot]/.well-known/[randomFile]?

Anyone know a way around this? I can delete the .netcore app, then run the SSL cert installation, but this installation needs to happen every 2-3 months, and since it's manual I'd prefer to figure out how to do it the right way.

like image 933
Kritner Avatar asked Jul 16 '16 00:07

Kritner


1 Answers

I found the information I needed here: https://docs.asp.net/en/latest/fundamentals/static-files.html

Basically in my Statup.cs I needed to change this:

        // allows for the direct browsing of files within the wwwroot folder
        app.UseStaticFiles();

        // MVC routes
        app.UseMvc(routes => 
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

to this:

        // allows for the direct browsing of files within the wwwroot folder
        app.UseStaticFiles();

        // Allow static files within the .well-known directory to allow for automatic SSL renewal
        app.UseStaticFiles(new StaticFileOptions()
        {
            ServeUnknownFileTypes = true, // this was needed as IIS would not serve extensionless URLs from the directory without it
            FileProvider = new PhysicalFileProvider(
                    Path.Combine(Directory.GetCurrentDirectory(), @".well-known")),
            RequestPath = new PathString("/.well-known")
        });

        // MVC routes
        app.UseMvc(routes => 
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

EDIT - Note that this directory ".well-known" was created only on the web server, when I started developing again locally, I was getting errors because the ".well-known" directory did not exist. So now I just have an empty directory in my project, but at least my SSL renewal is automated! :D

like image 175
Kritner Avatar answered Nov 15 '22 08:11

Kritner