EF Core error: The certificate chain was issued by an authority that is not trusted.

I am doing local development of a .NET 9 Web API. It’s using a local instance of SqlServer Express with Entity Framework Core. However, after setting up my DBContext and generating my Migrations for the first time, when I went to run the dotnet ef database update command, I received the follow error message:

Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 – The certificate chain was issued by an authority that is not trusted.)

While this looks intimidating, it’s actually pretty easy to fix. The problem was with my ConnectionString. The ConnectionString looked like this:

"ConnectionStrings": {
    "default": "Server=MYPC\\SQLEXPRESS;Database=DemoDB;User Id=SA;Password=SuperSecretPassword;"
  }

Unfortunately for local development, I was missing 2 things Trusted_Connection=True;TrustServerCertificate=True;. Once I made my ConnectionString look like this:

"ConnectionStrings": {
    "default": "Server=MYPC\\SQLEXPRESS;Database=DemoDB;User Id=SA;Password=SuperSecretPassword;Trusted_Connection=True;TrustServerCertificate=True;"
  }

All I had to do was rerun my dotnet ef database update command and everything functioned as planned.

Debugging C# Web API for first time in VS Code — fix “don’t have an extension for debugging XML” error

I am trying to run the VS Code debugger for the first time for my .NET 9 web api. When I hit F5 it doesn’t start debugging and I get this message:

You don’t have an extension for debugging XML. Should we find a XML extension in the Marketplace?

So to fix this issue, I asked Github Copilot what to do to make it work. This was it’s response.

Step-by-step solution:

1. Install the official C# extension (if not already done).
2. Avoid focusing on your .csproj file when starting debugging—open one of your C# source files.
3. Create a launch configuration file to explicitly start debugging your web API.

And this was the launch.json file it created.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": ".NET Launch Web API",
      "type": "coreclr",
      "request": "launch",
      "preLaunchTask": "build",
      "program": "${workspaceFolder}/dotnetapivue3.Server/bin/Debug/net9.0/dotnetapivue3.Server.dll",
      "args": [],
      "cwd": "${workspaceFolder}/dotnetapivue3.Server",
      "stopAtEntry": false,
      "serverReadyAction": {
        "action": "openExternally",
        "pattern": "\\bNow listening on:\\s+(https?://\\S+)"
      },
      "env": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  ]
}

I added the file to my project then, opened a controller file. Launched the debugger. After that, the project loaded as expected and I was able to set break points for debugging.

Add a mime-type to an ASP.NET Core (.NET 6) app

If you have a .NET 6 app with static files and you put in a file with a non-web standard extension somewhere in your WWWROOT you will possibly get a 404 if you try to access the file. This is because the app.UseStaticFiles(); option you add to your Program.CS file has a predefined list of files it will deliver from your site. For security reasons, anything not on this list gets blocked.

But the good news is, you can tell the app to add your file type to the Green Lit list. You just need to add a CotentTypeProvider to the StaticFileOptions passed to your UseStaticFiles method. In the code sample below, I am adding a proprietary .JSON file format with the extension of .RDLX-JSON which is blocked by default.

You still need to make a second, empty call for app.UseStaticFiles(); to have it serve the default filetypes.

var provider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();

provider.Mappings.Add(new KeyValuePair<string, string>(".rdlx-json", "application/json"));

app.UseStaticFiles(new StaticFileOptions()
{
    ContentTypeProvider = provider
});

app.UseStaticFiles();

Now, when you rebuild your app, your file should be accessed via a URL like other web files.

How to check which version of .Net Core you have installed.

Open a command prompt and enter the version command.

dotnet --version
dotnet version

This should return a version number. If you get something like the message below, .Net Core is not installed.

'dotnet' is not recognized as an internal or external command,
operable program or batch file.

You can also issue the info command to get a lot more information about your environment.

dotnet --info
dotnet info

If you want to actually look at the different .Net SDKs which you have installed, they can be found here on a Windows machine:

C:\Program Files\dotnet\sdk