adam78
adam78

Reputation: 10078

Retrieve user details from Active Directory from within an ASP MVC 5 application

I have an MVC 5 intranet application set up with windows authentication. I am new to Active Directory. How do I query Active Directory from within my MVC application to retrieve the currently authenticated users details such as email add, telephone etc - so basically with windows authentication all I have is the username?

Some code would help i.e connecting to AD etc?

Upvotes: 1

Views: 5299

Answers (1)

smr5
smr5

Reputation: 2793

Since you have the username, you can query Active Directory and grab the necessary information you need for the user. Here's a simple call to AD using AccountManagement namespace.

...
string UserName = "yourUserName";
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourdomain.com")
{
   using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName)
   if (user != null)
   {
      //user will contain information such as name, email, phone etc..
   }
}

Upvotes: 2

Related Questions