How do I know that my Windows user is a domain user?

Do I need to find out that my windows currentuser is a domain user or local user? I get My CurrentPrincipal with this:

System.Threading.Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); 
+4
source share
2 answers

Usually a Windows NON domain user does not have a UPN name. As below If I run WHOAMI with / upn:

 C:\>WHOAMI /UPN ERROR: Unable to get User Principal Name (UPN) as the current logged-on user is not a domain user. 

Based on this, using the code below, we can determine whether the user is a domain user or not.

  var upnName = System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName; if (upnName == null) { //not a domain user } 

Another long way would be to get a SAM username. Then, using DirectoryEntry, DirectorySearcher and other AD APIs, iterate through the entire federated domain of the machine and find if the user is in any of the domain that we iterate.

Find all machine domains ex1 and ex2

Here's how to get domain user information from Active Directory in C #

+3
source

I do not have AD at my disposal, but I would expect this to work:

 public bool IsDomainUser() { bool isDomain = false; foreach (var grp in WindowsIdentity.GetCurrent().Groups) { if (grp.IsValidTargetType(typeof(SecurityIdentifier))) { SecurityIdentifier si = (SecurityIdentifier)grp.Translate(typeof(SecurityIdentifier)); // not sure if this is right, but I'm not in a domain and don't have this SID // I do have LocalSID however, so you might need to turn it around, // if you have LocalSid you are not using a domain acount if (si.IsWellKnown(WellKnownSidType.BuiltinDomainSid)) { isDomain = true; } } } return isDomain; } // for debugging list SIDS for current user and its groups foreach (var obj in Enum.GetValues(typeof(WellKnownSidType))) { if (WindowsIdentity.GetCurrent().User.IsWellKnown((WellKnownSidType)obj)) { Debug.WriteLine("User:" + obj.ToString()); } foreach (var grp in WindowsIdentity.GetCurrent().Groups) { if (grp.IsValidTargetType(typeof(SecurityIdentifier))) { SecurityIdentifier si = (SecurityIdentifier) grp.Translate(typeof(SecurityIdentifier)); if (si.IsWellKnown((WellKnownSidType)obj)) { Debug.WriteLine("Grp: " + grp.ToString() + " : " + obj.ToString()); } } } } 
0
source

All Articles