Getting Started with Blazor WASM App with Authentication Library

30 Apr 20243 minutes to read

This article provides a step-by-step instructions for building and securing Blazor WebAssembly Standalone App with Blazor WebAssembly Authentication library using Visual Studio.

Prerequisites

Create a new Blazor WebAssembly Standalone App in Visual Studio

You can create a Blazor WebAssembly Standalone App using Visual Studio via Microsoft Templates or the Syncfusion Blazor Extension by setting the Authentication type to Individual Accounts. This selection adds authentication support and doesn’t result in storing users in a database.

Configure the application with Google Auth 2.0 OIDC

  • Set up Google OAuth 2.0 authentication. For more information, refer here

  • Now, replace the appsettings.json with the following code which contains code to configure the application with Google OAuth 2.0.

{
  "Local": {
    "Authority": "https://accounts.google.com/",
    "ClientId": "2...7-e...q.apps.googleusercontent.com",
    "PostLogoutRedirectUri": "https://<example.com>/authentication/logout-callback",
    "RedirectUri": "https://<example.com>/authentication/login-callback",
    "ResponseType": "id_token"
  }
}
  • Replace the ClientId with the OAuth 2.0 client ID of your created project.

OAuth 2.0 client ID

  • The RedirectUri (https:///authentication/login-callback) should be registered in the Google APIs console as mentioned below.

OAuth RedirectUri

Install Syncfusion Blazor Calendars and Themes NuGet in the App in the App

Here’s an example of how to add Blazor Calendar component in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search and install Syncfusion.Blazor.Calendars and Syncfusion.Blazor.Themes. Alternatively, you can utilize the following package manager command to achieve the same.

Install-Package Syncfusion.Blazor.Calendars -Version 26.2.4
Install-Package Syncfusion.Blazor.Themes -Version 26.2.4

NOTE

Syncfusion Blazor components are available in nuget.org. Refer to NuGet packages topic for available NuGet packages list with component details.

Register Syncfusion Blazor Service

Open ~/_Imports.razor file and import the Syncfusion.Blazor and Syncfusion.Blazor.Calendars namespace.

@using Syncfusion.Blazor
@using Syncfusion.Blazor.Calendars

Now, register the Syncfusion Blazor Service in the ~/Program.cs file of your Blazor WebAssembly Standalone App.

....
using Syncfusion.Blazor;
....
builder.Services.AddSyncfusionBlazor();
....

Add stylesheet and script resources

The theme stylesheet and script can be accessed from NuGet through Static Web Assets. Reference the stylesheet reference in the <head> section and the script reference at the end of the <body> as follows:

  • For .NET 8 Blazor WebAssembly Standalone app, include it in the ~/Components/App.razor file.
<head>
    ....
    <link href="_content/Syncfusion.Blazor.Themes/bootstrap5.css" rel="stylesheet" />
</head>

<body>
    ....
    <script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>
</body>

NOTE

Check out the Blazor Themes topic to discover various methods (Static Web Assets, CDN, and CRG) for referencing themes in your Blazor application. Also, check out the Adding Script Reference topic to learn different approaches for adding script references in your Blazor application.

Add Syncfusion Blazor component

Add the Syncfusion Blazor Calendar component in the ~/Pages/Home.razor file under AuthorizeView.

<AuthorizeView>
    <Authorized>
        <SfCalendar TValue="DateTime" />
    </Authorized>
    <NotAuthorized>
        <h1>Authentication Failure!</h1>
        <p>You're not signed in.</p>
    </NotAuthorized>
</AuthorizeView>
  • Press Ctrl+F5 (Windows) or +F5 (macOS) to launch the application. This will render the Syncfusion Blazor Calendar component in your default web browser.

Blazor Calendar Component

See Also