Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add .well-known to asp.net core

I want to have a .well-known directory in my root for letsencrypt renewals.

I have added a route to .well-known like so:

 app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @".well-known")),
            RequestPath = new PathString("/.well-known"),
            ServeUnknownFileTypes = true // serve extensionless file
        });

I added a direcotry inside my wwwroot called .well-known but it never gets copied to the output when I publish.

I tried adding a file to it and also edit csproj:

  <ItemGroup>
    <Content Include="wwwroot\.well-known\" />
  </ItemGroup>

Every time I publish I have to manually create the directory. How do I get it automatically added to the wwwroot?

like image 707
Guerrilla Avatar asked Apr 02 '17 09:04

Guerrilla


3 Answers

I tried adding a file to it and also edit csproj:

<ItemGroup>
  <Content Include="wwwroot\.well-known\" />
</ItemGroup>

You can't copy over folders via Content, only files. You have to change it to

<ItemGroup>
  <Content Include="wwwroot\**">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
<ItemGroup>

and like mentioned in the comments, you need to put an empty dummy file inside.

like image 95
Tseng Avatar answered Sep 27 '22 17:09

Tseng


Another approach is to make a controller - if you have complex rules - or the file varies by domain (as it does for certain types of verification tokens).

public class WellKnownFileController : Controller
{
    public WellKnownFileController()
    {

    }

    [Route(".well-known/apple-developer-merchantid-domain-association")]
    public ContentResult AppleMerchantIDDomainAssociation()
    {
        switch (Request.Host.Host)
        {
            case "www2.example.com":
                return new ContentResult
                {
                    Content = @"7B227073323935343637",
                    ContentType = "text/text"
                };

            default:
                throw new Exception("Unregistered domain!");
        }
    }
}

You can then just hit .well-known/apple-developer-merchantid-domain-association and get this controller.

Of course you can load the file from disk or whatever you need to do - or have a passthrough.

like image 21
Simon_Weaver Avatar answered Sep 27 '22 19:09

Simon_Weaver


you can add the below code to the MyProject.csproj file

  <ItemGroup>
    <Content Include=".well-known\acme-challenge\**">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
like image 20
Alper Ebicoglu Avatar answered Sep 27 '22 17:09

Alper Ebicoglu