Implementing Active Directory in a .NET application with Multiple Roles

August 18, 2021

The need for a directory of users arises when various devices are used on the same network. It is crucial to locate the directory on one central source, known as Active Directory. It helps validate and authenticate multiple users accessing all resources on the domain with a single sign-on.

In this blog, we will demonstrate how to implement Active Directory in a .NET application.

Login

What is Active Directory?

Active Directory is a database adapted by Microsoft to manage multiple devices on a single network. It also can be defined as a set of services that connect users with network resources they need to accomplish projects. In order to obtain access to devices, the network users require it to be validated.

Let us consider a C# .NET application to validate the users using Active Directory on the login page, before implementing Active Directory, we use ASP.NET membership to validate a user on the Login page. The code should look similar to the code below.

Code

Now, to create our own function to validate the user through Active Directory, we use the code below.

Code

VerifyUserAD accepts three parameters, namely, Username, Password and ReturnMsg. ReturnMsg returns the error message if the validation of the user fails against the Active Directory. Refer to the code below.

Code

Once the user is validated, the function VertifyUserAD returns an ‘authenticated’ message, based on which the user can take the following relevant actions.

To entirely bypass the ASP .NET membership, use the Active Directory to validate the user. For an existing application, users can maintain and access a copy of Users in the database, meaning there is no need to modify the whole application end-to-end.

Role-based implementation

For role-based implementation, we use the DirectorySearcher class to fetch the property ‘memberOf’ for that user in Active Directory, as shown in the code below.

Code

This enables users to find the list of Groups in which the user is assigned using the SearchResult.

Search result

Use the command below to install Directory Services using the package manager console.

Install-Package System.DirectoryServices

Hope this blog will help you in the implementation of Active Directory in Dot Net application. For more information on .Net services, please visit https://development.ikf.in/metasys1/dot-net

Tags :

Category :