Search code examples
c#.netactive-directorywindows-identityuserprincipal

How do I compare UserPrincipal with WindowsIdentity?


When binding a user to a Windows Account Im using the UserPrincipal SamAccountName according to this post(binding saved in database). Then when doing a login I need to check this binded user(from database) against the loggedin Windows Account and this is done with a WindowsIdentity objeckt.

The problem is that WindowsClient.Name will state Group\SamAccountName instead of just the SamAccount? It would be good to use the group but the UserPrincipal does not seem to return this?

So how should I match thay to? Should I just remove the Group in WindowsClient.Name or is there a way to add it in the UserPrincipal?


Solution

  • There are a few options here:

    1. Don't compare principals using SamAccountName only, theyre only unique within a single domain, use SID instead: UserPrincipal.Sid and WindowsIdentity.User

    2. Don't use WindowsIdentity to get the current user, use UserPrincipal.Current instead.