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

APIs 101: Things you need to know

WordPress Revision History

Is Ruby on Rails Dying, or Is It on the Right Track?

Why I’m into dual screen app development?

Stranger Things: Why Engineering Teams Slow as They Grow

Short story on how to auto-login Wordpress from a single email link

Step by step instructions to utilize FileBot to put together your Plex video records

Building a realtime server backend using the Orleans Actor system, Dotnet Core and Server-side…

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

Don’t forget to take a step back in your start-up. Database Back-ups in your .NET Application

Integrate Firebase Real Time Database with  .Net

Firebase + .Net integration

How to move and delete a single message from RabbitMQ (with C#)

dotnet retrieve JSON (nested object) + file without using a customer model binder