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.

Cloning Bitbucket Repositories with GitHub Desktop: Get Authentication to Work

To authenticate against Bitbucket and clone repositories using GitHub Desktop, you can follow these 3 steps:

Step 1: Enable Git Credential Manager in GitHub Desktop

  1. Open GitHub Desktop.
  2. Go to File > Options > Advanced (on Windows) or GitHub Desktop > Preferences > Advanced (on macOS).
  3. Select the checkbox for Use Git Credential Manager.

Step 2: Create a Personal Access Token in Bitbucket

  1. Log in to your Bitbucket account.
  2. Go to Personal Bitbucket settings in the settings dropdown.
  3. Select App passwords (the side menu may be collapsed).
  4. Click Create app password.
  5. Under the Details section, enter a label for your password.
  6. Under Permissions, select Read and Write in the Repositories section.
  7. Click Create to generate a new token.
  8. Copy the token to your clipboard. NOTE: You will not be able to view this again later.

Step 3: Clone Your Bitbucket Repository in GitHub Desktop

  1. Open GitHub Desktop.
  2. Go to File > Clone Repository > URL.
  3. Enter the HTTPS clone URL of your Bitbucket repository.
  4. When prompted for authentication, enter your Bitbucket username and paste the token you copied as your password.
  5. Click Save and Retry to successfully clone the repository to your local machine.

This setup should allow you to authenticate against Bitbucket and clone repositories using GitHub Desktop.

How do I change my name and email for my global git config?

You can change your name and email for your global Git configuration using the following commands in your terminal:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Of course, you will want to replace "Your Name" with your desired name and "your.email@example.com" with your desired email address. This will update your global Git configuration, and these details will be used for all your commits.

If you want to verify the changes, you can use:

git config --global --list

This command will list all your global Git configuration settings, including your updated name and email.

JavaScript DeepClone function created by ChatGPT and it works

One thing I hate dealing with is cloning JavaScript Objects. Well, deep cloning JavaScript Objects. So I decided to see if AI could write me a quick function.

And it did.

I asked ChatGPT “Can you write me a JavaScript function which will deep clone a JavaScript Object?”

And this was it’s response:

Certainly! You can create a deep clone function in JavaScript using recursion to clone nested objects and arrays. Here’s a sample function to deep clone a JavaScript object:

function deepClone(obj) {
  if (obj === null || typeof obj !== 'object') {
    return obj;
  }

  if (Array.isArray(obj)) {
    const cloneArr = [];
    for (let i = 0; i < obj.length; i++) {
      cloneArr[i] = deepClone(obj[i]);
    }
    return cloneArr;
  }

  const cloneObj = {};
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      cloneObj[key] = deepClone(obj[key]);
    }
  }

  return cloneObj;
}

You can use this deepClone function to create a deep copy of a JavaScript object. Here’s an example of how to use it:

const originalObj = {
  name: 'John',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'Exampleville',
  },
};

const clonedObj = deepClone(originalObj);

console.log(clonedObj);

The clonedObj will be a deep copy of the originalObj, so you can modify it without affecting the original object.

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.

VS Code: Sort lines of code in Ascending (or Descending) Order

Select the code you wish to sort in Visual Studio Code.

Then hit the key combination Ctrl+P and type the greater than sign (>). Next type sort and choose Sort Lines Ascending or choose the Descending option.

Now the lines you’ve previously selected will be sorted by the option you chose. One thing to keep in mind is that if you have lines which have breaks in them, the second line will also be sorted into the mix. Which may mess up formatting for things like CSS Property values.

How to pad a string in JavaScript

Sometimes you need your string to be a set length, like maybe you have numbers that all need to be 10 characters long regardless of the number value. Or maybe you have a credit card number and want to just show the last 4 digits while everything else is replaced with an asterisk (*). Regardless of the reason, JavaScript now has built in methods to add the padded characters to either the start or the end of your string. Let’s begin with the padding at the start to get the idea going.

Say you have an invoice system that needs it’s invoice numbers to be 10 characters, but you are only up to invoice number 4532. You’ll want to have six zeros prepended to the invoice number for display. To do this, we will use JavaScript’s padStart() method. This method takes two parameters: targetLength & padString. targetLength is how long the string will be once the desired number of characters have been added. padString is the character that will be used for padding; if none is supplied, JavaScript will use " " (U+0020 'SPACE').

Using the invoice example above, your code would look like this:

let invoiceNumber = "4532"
invoiceNumber.padStart(10, '0')
// Output: "0000004532"

The padEnd() method works the same way but appends the padding character to the end of the string. So, our example would look like this instead:

let invoiceNumber = "4532"
invoiceNumber.padEnd(10, '0')
// Output: "4532000000"

More Information:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd

How to subtract or add days from a Date in JavaScript?

If you have a date in JavaScript and need to subtract (or add) a number of days to that date, you will want to use JavaScript’s built in setDate() method. This will set the day of the month for a given date.

NOTE:
setDate() is based on local time. If you want to work with UTC time then use setUTCDate()

So, if you wanted to set the day of the month to be the 15th of the current month you would start with today’s date and change the date like so:

var d = new Date();
d.setDate(15);
// Sun Aug 15 2021 16:49:25 GMT-0500 (Central Daylight Time)

Using this approach combined with the getDate() method (which returns the current month’s date for a given date), you can substract or add days. If you want to get the date which is Today – 5 days then you could use:

var d = new Date();
d.setDate(d.getDate()-5);
// Tue Aug 10 2021 16:56:31 GMT-0500 (Central Daylight Time)

More Information:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate