DavidVdd
DavidVdd

Reputation: 1018

Retrieving AD information inside a mvc3 c# project

I recently started a project where I have to authenticate users against the Active Directory.

Using authentication mode="Windows". I managed to login and retrieve the user name.

@Context.User.Identity.Name

Now I was wondering how I could get the GUID from the user that is logged in.

I want to use the GUID from the active directory in my own database to make its members unique.

Upvotes: 2

Views: 792

Answers (1)

marc_s
marc_s

Reputation: 754518

Since you're on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

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

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, User.Identity.Name);

if(user != null)
{
   // do something here ... like grabbing the GUID
   var userGuid = user.Guid;
}

The new S.DS.AM makes it really easy to play around with users and groups in AD:

Upvotes: 2

Related Questions