Getting Started With Authentication And Authorization Using Blazor Server Side

In this article, you will learn about authentication and authorization using Blazor server-side.

The wait is over and yes, now we can add the ASP.NET Core the Authentication and Authorization functions to Blazor application. In this article, we will see in detail on how to use Authentication and Authorization using Blazor Server-Side application. Now, you can directly use the Authentication and Authorization for Blazor Server Side application. The new preview version of .NET Core 3.0 and the latest Visual Studio 2019 allow us to use the ASP.NET identity to work with a Blazor application.
 
Here, we will see how to -
  • Create a database in SQL Server and use it for ASP.NET table creation.
  • Display a menu to authenticated and authorized members
Getting Started With Authentication And Authorization Using Blazor Server Side
 
For non-authenticated and authorized members, display a different menu.
 
Getting Started With Authentication And Authorization Using Blazor Server Side 
 
ASP.NET Identity allows us to add login functionality to our system. Here, in this demo, we will be using SQL Server to store the user details and profile data. We will use ASP.NET Identity for new user registration, login, and to maintain the user profile data. If we talk about the login, the important part is whether the logged in user is authenticated and also authorized to view the pages.
 

Authentication and Authorization


Authentication

Check for the Valid User. Here, the question is how to check if a user is valid or not. When a user comes to a website for the first time, he/she will register for that website. All their information, like username, password, email, and so on will be stored in the website database. When a user enters his/her userID and password, the information will be checked with the database. If the user has entered the same userID and Password as in the database, then he or she is a valid user and will be redirected to the website's home page. If the user entered UserID or Password that does not match the database, then the login page will give a message, something like “Enter valid Username or Password”. The entire process of checking whether the user is valid or not for accessing the website is called Authentication. 
 
Authorization

Once the user is authenticated, they need to be redirected to the appropriate page by his/her role. For example, when an Admin is logged in, then need to be redirected to the Admin Page. If an Accountant is logged in, then he/she needs to be redirected to his Accounts page.
 
Prerequisites

Make sure you have installed all the prerequisites on your computer. If not, then download and install them all, one by one.
 
Prerequisites
Getting Started With Authentication And Authorization Using Blazor Server Side
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Getting Started With Authentication And Authorization Using Blazor Server Side 
Step 1 - Create a Database
 
Firstly, we will create a Database and set the connection string in the appsettings.json file for DefaultConnection with our new database connection. We will be using this database for ASP.NET Core Identity table creation.
 
Create Database
 
Run the following script to create our database. 
  1. USE MASTER     
  2. GO     
  3. -- 1) Check for the Database Exists .If the database is exist then drop and create new DB     
  4. IF EXISTS (SELECT [nameFROM sys.databases WHERE [name] = 'BlazorDB' )     
  5. DROP DATABASE BlazorDB     
  6. GO     
  7.      
  8. CREATE DATABASE BlazorDB     
  9. GO     
  10.      
  11. USE BlazorDB     
  12. GO    
After running the DB Script we can see as the Database has been created and tables have not yet been created.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Step 2 - Create a Blazor Server Side
 
After installing all the prerequisites listed above and ASP.NET Core Blazor extension, click Start >> Programs >> Visual Studio 2019 >> Visual Studio 2019 on your desktop. Click New >> Project. 
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Click on ASP.NET Core Web Application and click "Next".
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Enter your project name and click the "Create" button.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Now, we can see that for ASP.NET Core 3.0 has been listed. We Select the Blazor (Server Side) and then we click on Change Authentication to set our Authentication for our project.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Here we select the Individual User Account to store all our User details to SQL server.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
After creating ASP.NET Core Blazor application, wait for a few seconds. You will see the below structure in the Solution Explorer. Here, we can see pages folder which contains all our razor pages and Shared folder which is similar to our ASP.NET MVC shared folder which will contain the Navigation Menu page, MainLayout page for the content display and the LoginDisplay page which will be used for new user register and login to the site .appsetting.json page is to set our database connection string.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Updating appsettings.json 
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
In the appsettings.json file, we can find the DefaultConnection Connection string. Here, in the connection string, change your SQL Server Name, UID and PWD to create and store all user details in one database.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Step 3 - Register and create your first User
 
Now, our Blazor web application is ready for users to register in our website and also users can log in to our system after registration. Build and run your application to register your first user.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Click on the Register link to register our first User.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Migration
 
When we click on the Register button we can see the below page. Don’t panic with this page as for the first time we run it we need to do the Migration, just click on the Apply Migrations button. 
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
We can see the confirmation as Migration Applied and then click on the Try refreshing the page message.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Refresh the page and we can see the newly registered user has been logged in to our web site.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
We can also update the user details by clicking on the user name at the top of the site.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Refresh the Database
 
When we refresh our database, we can see all the Identity tables have been created.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Step 4 - Displaying Menu by Authentication
 
Now let’s see how to show the menu for the non-authenticated user and Authenticated users. For this, first, we add a Razor page and name it as Userpage like below.
 
Right-click on the Pages folder and click "Add New Pages".
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Name it as UsePage and click "Add".
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
In the page, we just add an H1 tag and some text as you are not Loggedin.
 
Getting Started With Authentication And Authorization Using Blazor Server Side

Hide and show menu by Authentication

 
Now, we will show and hide the menu by user authentication and authorization. For doing this, first, we open the menu page. You can see NavMenu.razor under Shared folder.
 
Getting Started With Authentication And Authorization Using Blazor Server Side
 
Now we can see as by default 3 menu has been added as Home, Counter and Fetch data. We will show the Counter and Fetch data menu only for the Authenticated and Authorized users.
 
And for non-authorized users, we will show our newly created Userpage. For this, here, we will be using the code like below.
  1. <AuthorizeView>  
  2.             <Authorized>  
  3.                 <li class="nav-item px-3">  
  4.                     <NavLink class="nav-link" href="counter">  
  5.                         <span class="oi oi-plus" aria-hidden="true"></span> Counter  
  6.                     </NavLink>  
  7.                 </li>  
  8.                 <li class="nav-item px-3">  
  9.                     <NavLink class="nav-link" href="fetchdata">  
  10.                         <span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data  
  11.                     </NavLink>  
  12.                 </li>  
  13.             </Authorized>  
  14.             <NotAuthorized>  
  15.                 <li class="nav-item px-3">  
  16.                     <NavLink class="nav-link" href="NonUser">  
  17.                         <span class="oi oi-list-rich" aria-hidden="true"></span> Non Authoraized Menu  
  18.                     </NavLink>  
  19.                 </li>  
  20.             </NotAuthorized>  
  21.         </AuthorizeView>  
Here we can see as we have used the <Authorizeview> tag. This tag is used to check if the user is authorized or not. Inside the < Authorizeview> tag we used the <Authorized> tag for checking if the user is Authorized and displayed the message, in the same way  use <NotAuthorized> tag to display non-authorized users. Here we show and hide the menus based on Authorized and NotAuthorized.
 

Conclusion

 
Firstly, create a sample BalzorDB Database in your SQL Server. In the appsettings.json file, change the DefaultConnection connection string with your SQL Server Connections. Here, we have used our needed database to store all the ASP.NET identity information. In this way, we can also create our other needed tables in the database and make relations to store user ids in needed tables.