Protect the Blazor WebAssembly Application UI
Learn to protect the UI of our Blazor WebAssembly application.
We’ll learn how to grant access only to authenticated users in the Blazor WebAssembly application.
Enable authorization support
To start, we need to enable support for authorization in our Blazor WebAssembly application. We can do this by using a few Razor components.
Let’s add the references to the needed namespaces for authorization support. Open the _Imports.razor
file in the Client
folder and add the two namespace references, as highlighted in the following code snippet:
Press + to interact
@using System.Net.Http@using System.Net.Http.Json@using Microsoft.AspNetCore.Components.Authorization@using Microsoft.AspNetCore.Authorization@using Microsoft.AspNetCore.Components.Forms@using Microsoft.AspNetCore.Components.Routing@using Microsoft.AspNetCore.Components.Web@using Microsoft.AspNetCore.Components.Web.Virtualization@using Microsoft.AspNetCore.Components.WebAssembly.Http@using Microsoft.JSInterop@using MyBlazorWasmApp.Client@using MyBlazorWasmApp.Client.Shared
We added the following two lines:
- Line 3: This is a reference to the
Microsoft.AspNetCore.Components.Authorization
namespace, which makes authorization-related components available to our application. - Line 4: This is a reference to the
Microsoft.AspNetCore.Authorization
namespace, which makes ASP.NET authorization support