Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (150).NET Core  (24)Angular  (42)ASP.NET  (48)ASP.NET Core  (57)ASP.NET MVC  (84)Azure  (28)Blazor  (74)DocIO  (18)Essential JS 2  (67)Essential Studio  (161)Flutter  (85)JavaScript  (141)Microsoft  (101)PDF  (54)React  (32)Succinctly series  (120)Syncfusion  (459)TypeScript  (30)Uno Platform  (2)UWP  (4)Vue  (26)Webinar  (18)Windows Forms  (54)WPF  (108)Xamarin  (119)XlsIO  (22)Other CategoriesBarcode  (4)BI  (29)Bold BI  (3)Build conference  (6)Business intelligence  (53)Button  (4)C#  (104)Chart  (45)Cloud  (9)Company  (445)Dashboard  (6)Data Science  (3)Data Validation  (3)DataGrid  (36)Development  (245)Doc  (7)DockingManager  (1)eBook  (91)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (16)Extensions  (11)File Manager  (3)Gantt  (8)Gauge  (6)Git  (3)Grid  (25)HTML  (9)Installer  (2)Knockout  (2)LINQPad  (1)Linux  (1)M-Commerce  (1)Metro Studio  (11)Mobile  (188)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (19)Performance  (3)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (5)Road Map  (9)Scheduler  (20)SfDataGrid  (8)Silverlight  (21)Sneak Peek  (14)Solution Services  (2)Spreadsheet  (5)SQL  (5)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (9)Tips and Tricks  (44)UI  (106)Uncategorized  (68)Unix  (2)User interface  (67)Visual State Manager  (1)Visual Studio  (17)Visual Studio Code  (9)Web  (163)What's new  (108)Windows 8  (19)Windows App  (1)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
Easily Create a Blazor Server-Side App with Authentication

Easy Steps to Create a Blazor Server App with Authentication

In this article, we are going to see how to create a new Blazor server application with authentication. We will use the built-in AuthenticationStateProvider service to enable authentication. Then, we will obtain the authentication state data from the ASP.NET Core HttpContext.User property.

Let’s see about them along with code examples!

Prerequisites

Create a Blazor server application

Follow these steps to create a Blazor server-side app with authentication.

Step 1:  First, open the Visual Studio 2019 and click the Create a new project option as shown in the following screenshot.
Open Visual Studio 2019 and click the Create a new project option

Step 2: Next, choose the Blazor App option and click Next.
Choose the Blazor App option

Step 3: Select Blazor Server App from the list. To enable authentication for the Blazor server-side app, select the Configure for HTTPS check box in the Advanced section.  Then, click the Change link in the Authentication section.

Select Blazor Server App option, Configure for HTTPS check box in the Advanced section and click the Change link in the Authentication sectionStep 4: A new dialog will appear.  In that, select the Individual User Accounts option and then click OK.
Select the Individual User Accounts option and then click OK

Step 5: Click the Create button to create the Blazor server-side application. Once the Blazor application is created, run the project. Now, click the Register button in the header section.

Refer to the following screenshot.
Click the Register button in the header section

Step 6: Enter your authentication details in the appropriate fields. Then, click Register.
Enter your authentication details in the appropriate fields and then click Register

Step 7: After successfully registering the email ID and password, select Apply Migrations to set up the account details based on the database.
Select Apply Migrations

Step 8: Now, the home page will open. Refresh the page to see the home page of your application with a logged-in user’s details. The Register link will be replaced with the user’s mail ID. The Login link will be replaced with the Log out link in the header section.
Home Page of your Blazor server-side application

AuthorizeView component

We’ll use the built-in AuthorizeView component in the Blazor application to show the user authentication state in the UI. We can also use the AuthorizeView to get the user details.

This component provides the following two child components:

  • Authorized: Denotes the user is authorized (logged in).
  • NotAuthorized:Denotes the user is not authorized (logged out).

Refer to the following code.

<AuthorizeView>
<Authorized>
<span> Hello, @context.Employee.Identity.Name!</span>
<form method="post">
<button type="submit">Sign out</button>
</form>
</Authorized>
<NotAuthorized>
<span> Welcome! Please sign up below </span>
<a href="Employee/Register">Register</a>
<a href="Employee/Signin">Sign in</a>
</NotAuthorized>
</AuthorizeView>

Custom AuthenticationStateProvider

Also, Blazor provides a built-in custom AuthenticationStateProvider service. You can implement this class and override the GetAuthenticationStateAsync method to implement custom authentication.

Refer to the following code.

using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Authorization;

public class CustomAuthStateProvider : AuthenticationStateProvider
{
    public override Task<AuthenticationState> GetAuthenticationStateAsync()
    {
        var identity = new ClaimsIdentity(new[]
        {
            new Claim(ClaimTypes.Name, "exampleuser"),
        }, "user authentication type");

        var user = new ClaimsPrincipal(identity);

        return Task.FromResult(new AuthenticationState(user));
    }
}

Difference between authentication and authorization

Authentication: Authentication is done before authorization. It is the process of validating that the users are who they claim to be. It determines whether the person is a registered user.

Authorization: Authorization is done after authentication. It is the process of giving permission to the user to access a resource.

References

For more details, refer to our Blazor server-side app with authentication demo and Blazor server authentication documentation.

Conclusion

Thanks for reading! In this post, we explained with a demo how to create a new Blazor server-side app with authentication using the built-in AuthenticationStateProvider service. Try out the steps provided in this blog post and leave your feedback in the comments section below!

Essential Studio for Blazor offers the largest selection of components for the Blazor platform. It has popular components like ChartsDataGridSchedulerDiagramDocument Editor, and Maps. It also includes unique file-format libraries for manipulating ExcelWordPDF, and PowerPoint files. Use them to build world-class applications!

Try our Blazor components by downloading a free 30-day trial, or check out our NuGet package. Feel free to peruse our online examples and documentation to explore other available features.

You can also contact us through our feedback portalsupport forums, or Direct-Trac. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin

Leave a comment

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top