I wanted to take the time to write up this article since I found it useful when working on Blazor Apps. Althought this is not Sitecore related, I think it will be useful if we need to build an admin .NET portal. Additionally most organizations already have Entra ID if they use Microsoft apps and building Entra ID Authentication wont add costs like other Authentication methods would.
To get started make sure the following packages are available (using NuGet) in your solution.
In your Program.cs file, add the following
In my example, I am going to put my entire application behind the Authentication, except the home page. So users cannot get to any page in the application un-authenticated except the home page.
In your App.razor file, add the following (surround your existing Routes with CascadingAuthenticationState)
In your Home.razor file, add the following
Add a new Login.razor file with the following content
Lastly in your appsettings.json file add the following (or add this in your environmental variables for production apps)
Note:
You get the TenantId and ClientId by going to your Azure Portal Microsoft Entra ID and registering a new App. Once you register a new app you would note down the ClientId and use it in your application And lastly to test this out, make sure that you have the right redirect urls added to the list of "Redirect URI's." for the new app that you created. This is found by going to the Authentication section of your new App in your Azure portal. For example the following are ones that are common.
That's it. Simple way to add secure Entra ID Authentication to your Blazor App.
To get started make sure the following packages are available (using NuGet) in your solution.
In your Program.cs file, add the following
In my example, I am going to put my entire application behind the Authentication, except the home page. So users cannot get to any page in the application un-authenticated except the home page.
In your App.razor file, add the following (surround your existing Routes with CascadingAuthenticationState)
In your Home.razor file, add the following
Add a new Login.razor file with the following content
Lastly in your appsettings.json file add the following (or add this in your environmental variables for production apps)
Note:
You get the TenantId and ClientId by going to your Azure Portal Microsoft Entra ID and registering a new App. Once you register a new app you would note down the ClientId and use it in your application And lastly to test this out, make sure that you have the right redirect urls added to the list of "Redirect URI's." for the new app that you created. This is found by going to the Authentication section of your new App in your Azure portal. For example the following are ones that are common.
That's it. Simple way to add secure Entra ID Authentication to your Blazor App.
Comments
Post a Comment