How to request the current registration of AD user information in MVC 4

I have an MVC4 intranet page and want to get the homeDirectory attribute from Active Directory. I would like to know the fastest way to get an attribute from AD.

+4
source share
1 answer

Since you are using .NET 3.5 and above, you should check the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read more here:

Basically, you can define the context of a domain and easily find users and / or groups in AD:

 // set up domain context using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) { // find a user UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName"); if(user != null) { // do something here.... string homeDrive = user.HomeDrive; string homeDirectory = user.HomeDirectory; } } 

If you use Windows authentication in your ASP.NET MVC application, you can also get a registered user at the moment:

 UserPrincipal currentUser = UserPrincipal.Current; 

But most often in a web application it is something like the NETWORK SERVICE or IUSER_machineName user (and not your user in the browser) ...

The new S.DS.AM makes it very easy to play with users and groups in AD!

+4
source

All Articles