Windows Authentication & Authorization in Blazor Application

NuGet Installation
public void ConfigureServices(IServiceCollection services){services.AddRazorPages();services.AddServerSideBlazor();services.AddHttpClient<IProductService, ProductService>(client =>{client.BaseAddress = new Uri(“https://localhost:44340/");});services.AddAuthorizationCore();}
@using System.Net.Http@using Microsoft.AspNetCore.Authorization@using Microsoft.AspNetCore.Components.Authorization@using Microsoft.AspNetCore.Components.Forms@using Microsoft.AspNetCore.Components.Routing@using Microsoft.AspNetCore.Components.Web@using Microsoft.JSInterop@using EmployeeManagement.Web@using EmployeeManagement.Web.Shared
using EmployeeManagement.Web.Services;using Microsoft.AspNetCore.Components.Authorization;using System;using System.Collections.Generic;using System.Linq;using System.Security.Claims;using System.Threading.Tasks;namespace EmployeeManagement.Web.Auth{public class WinAuthStateProvider : AuthenticationStateProvider{public async override Task<AuthenticationState> GetAuthenticationStateAsync(){//Getting logged in windows user namevar user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;//Logic for fetching role from DB — Here you can get other details abou user from DB alsostring role = UserService.retRole(user.Split(‘\\’)[1]);//Generating new Authentication state with claims if user have any roleif (role != “” && role !=null){var WindowsAuth = new ClaimsIdentity(new List<Claim>() {new Claim(ClaimTypes.Name,user.Split(‘\\’)[1]),new Claim(ClaimTypes.Role,role)},”windows”);return await Task.FromResult(new AuthenticationState(new ClaimsPrincipal(WindowsAuth)));}else{var anonymous = new ClaimsIdentity();return await Task.FromResult(new AuthenticationState(new ClaimsPrincipal(anonymous)));}}}}
<div class=”top-row pl-4 navbar navbar-dark”><a class=”navbar-brand” href=””>EmployeeManagement.Web</a><button class=”navbar-toggler” @onclick=”ToggleNavMenu”><span class=”navbar-toggler-icon”></span></button></div><div class=”@NavMenuCssClass” @onclick=”ToggleNavMenu”><ul class=”nav flex-column”><AuthorizeView Roles=”Admin”><Authorized><li class=”nav-item px-3"><NavLink class=”nav-link” href=”” Match=”NavLinkMatch.All”><span class=”oi oi-home” aria-hidden=”true”></span> Home</NavLink></li></Authorized></AuthorizeView><AuthorizeView Roles=”User”><Authorized><li class=”nav-item px-3"><NavLink class=”nav-link” href=”/dashboard” Match=”NavLinkMatch.All”><span class=”oi oi-home” aria-hidden=”true”></span> Dashboard</NavLink></li></Authorized></AuthorizeView><AuthorizeView Roles=”Admin”><Authorized><li class=”nav-item px-3"><NavLink class=”nav-link” href=”/addUser” Match=”NavLinkMatch.All”><span class=”oi oi-home” aria-hidden=”true”></span> Add Users</NavLink></li></Authorized></AuthorizeView></ul></div>@code {private bool collapseNavMenu = true;private string NavMenuCssClass => collapseNavMenu ? “collapse” : null;private void ToggleNavMenu(){collapseNavMenu = !collapseNavMenu;}}
@page “/dashboard”@attribute [Authorize(Roles=”User”)]<h3>Dashboard</h3>@code {}

--

--

--

Technical Speaker | Computer Engineer | Full Stack Web Developer | ML Enthusiast | * Knowledge Shared = Knowledge² *

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Pareto Chart with Python

Candidates and Companies alike Want to Know: Which Programming Languages Devs Should Devs have in…

Functioning of Anypoint MQ DLQ in Mule 3

3 Interviews — 5 Questions

Context-sensitive code reviews

Mysql client for Linux and Mac

Custom Swatch for Material App Theme — primarySwatch

Python Basics-Object Oriented Programming

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vaibhav Bhapkar

Vaibhav Bhapkar

Technical Speaker | Computer Engineer | Full Stack Web Developer | ML Enthusiast | * Knowledge Shared = Knowledge² *

More from Medium

Implement gRpc Global Exception Handler in ASP.NET

.NET Wait Handles

How to Build Windows Desktop Barcode and QR Code Scanner in .NET 6

Using Exception.Data property to log user-defined information about exceptions