Easy Steps to Create a Blazor Server App with Authentication
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (219)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (917)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (149)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (631)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (595)What's new  (332)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
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 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 section

Syncfusion’s Blazor components suite is the expert’s choice for building modern web apps.

Step 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

Every property of the Syncfusion Blazor components is completely documented for easy use.

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.

See how Syncfusion Blazor components can be transformed into beautiful and efficient applications.

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:

Comments (1)

Thank you for this very well explained tutorial ! It’s been very useful !

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed