How to get active directory current user display name in asp net core

Introduction

ASP.NET is a popular programming language used for building web applications. One common requirement in web applications is to retrieve the current user's display name from the Active Directory. In this article, we will explore how to achieve this in ASP.NET Core.

Step 1: Install the required packages

Before we can start retrieving the current user's display name, we need to install the necessary packages. Open your ASP.NET Core in Visual Studio and to the NuGet . Search for the following packages and install them:


Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
System.DirectoryServices.AccountManagement

Step 2: Configure the Active Directory

Next, we need to configure the connection to the Active Directory. Open the appsettings.json file in your project and add the following configuration:


"ActiveDirectory": {
  "": "your-domain",
  "Username": "your-username",
  "Password": "your-password"
}

Replace “your-domain”, “your-username”, and “your-password” with the appropriate for your Active Directory.

Step 3: Retrieve the current user's display name

Now that we have the necessary packages and the Active Directory connection configured, we can retrieve the current user's display name. Add the following code to your ASP.NET Core controller:


using System.DirectoryServices.AccountManagement;

public class UserController : Controller
{
    private readonly IConfiguration _configuration;

    public UserController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public IActionResult Index()
    {
        using (var context = new PrincipalContext(ContextType.Domain, _configuration["ActiveDirectory:Domain"], _configuration["ActiveDirectory:Username"], _configuration["ActiveDirectory:Password"]))
        {
            var user = UserPrincipal.FindByIdentity(context, User.Identity.Name);
            var displayName = user.DisplayName;

             View(displayName);
        }
    }
}

The code above retrieves the current user's display name by creating a new instance of the PrincipalContext class and using the UserPrincipal.FindByIdentity method to find the user based on the current user's identity. The display name is then returned to the view.

Step 4: Display the current user's display name

Finally, we need to display the current user's display name in our view. Create a new view in your ASP.NET Core project and add the following code:


@model string

Welcome, @Model!

The code above displays the current user's display name using the @Model directive.

Conclusion

In this article, we have learned how to retrieve the current user's display name from the Active Directory in ASP.NET Core. By following the steps outlined above, you can easily incorporate this functionality into your web applications.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents