In this article, I will provide information on how to authorize the logged in Windows users using Windows Active Directory groups in Razor pages. In order to see how to create a .NET Core web application with Razor pages and retrieve data from SQL Server using Entity Framework, you can visit my
previous article.
Below are the software/concepts used in this document.
- Visual Studio 2019
- Razor Pages
- .Net Core 2.1
- Net Core Web Application
- C# Language
Introduction
The best practice for designing an application is to be very sure about the process by which we would be authenticating the user to log into the application and then, authorizing the user to use the functionalities of the application. In this article, I have already used Windows Authentication while creating the web application and now, I would be showcasing on how to use Windows Active Directory groups to authorize the logged in Windows user. Below is the step-by-step description on how to achieve this in .NET Core Razor Pages.
Open your project in Visual Studio 2019
In my case, I am opening the earlier created project where Razor pages are present.
Ways to achieve authorization in .NET Core Web Application
There are multiple ways to achieve authorization like using Custom role-based authorization, using policy-based authorization, simply calling method to check if the user is authorized to access the method or not, etc. Here, we will be discussing the below two ways.
- Using a custom role-based authorization.
- Using a custom method to check if logged in user is part of Windows AD Group or not.
Custom role-based authorization in .NET Core Web Application
- Create a new class which would extend the IClaimsTransformation.
- public class ClaimsTransformer : IClaimsTransformation
- {
- public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
- {
- var wi = (WindowsIdentity)principal.Identity;
- if (wi.Groups != null)
- {
- foreach (var group in wi.Groups)
- {
- try
- {
- var claim = new Claim(wi.RoleClaimType, group.Value);
- wi.AddClaim(claim);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- return Task.FromResult(principal);
- }
- }
- Add the below line to your configureService method in Startup.cs as you have created your own class extending the IClaimsTransformation.
- services.AddSingleton<IClaimsTransformation, ClaimsTransformer>();
- In your Controller, add the “Authorize” line to get the authorization in place for every method where you want to implement the authorization.
- [Route("api/[controller]/[action]")]
- [ApiController]
- public class MYListsController : ControllerBase
- {
- [Authorize(Roles = "Admin")]
- [HttpGet]
- [ActionName("GetMyList")]
- public bool GetMyList()
- Test the functionality by debugging. In my example, I am a part of “Admin” Windows AD Group. Hence, while debugging, I reach the “GetMyList” method. If I change the “Authorize” line to [Authorize(Roles = "Contribute")], it does not even reach the “GetMyList” method as I am not part of this Windows AD Group.
- Similarly, the Authorize line can be used for entire PageModel in Razor Pages as we don’t have a way as of now to authorize any action inside the Page Model.
Custom method to authorize the user in .NET Core Web Application
- Create a new class in your .NET Core web application and have a method to check if the current user is a part of AD Group or not. In my example, I have created a class with the name “Security”.
- public static class Security
- {
- public static bool IsInGroup(ClaimsPrincipal User, string GroupName)
- {
- bool check = false;
- var user = (WindowsIdentity)User.Identity;
- if (user.Groups != null)
- {
- foreach (var group in user.Groups)
- {
- check = group.Translate(typeof(NTAccount)).ToString().Contains(GroupName);
- if (check)
- break;
- }
- }
- return check;
- }
- }
- In your Controller, you can call this method to check the user and then, based on the output, you can extend the functionality.
- [HttpGet]
- [ActionName("GetMyList")]
- public bool GetMyList()
- {
- var check = Security.IsInGroup(User, "Admin");
- var check1 = Security.IsInGroup(User, "Contribute");
- return check;
- }.
- Test the functionality by debugging. In my example, I am a part of “Admin” Windows AD Group. Hence, while debugging, I am getting True for the first line but for the second line, (Security.IsInGroup(User,”Contribute”); I am getting False as I am not part of this Windows AD Group.
- Similarly, the Security.IsInGroup method can be called from PageModel as well in Razor Pages to authorize the user.
That is it. I hope you have learned something new from this article and will utilize this in your work.