Reputation: 10078
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
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