I need find out that my windows currentuser is domain user or local user? I get My CurrentPrincipal with this:
System.Threading.Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
Normally the windows NON domain user doesn't have a UPN name. Like below If I fire 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 below code we can find whether the user is domain user or not.
var upnName = System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName;
if (upnName == null)
{
//not a domain user
}
The other lengthy way would be to get the user name in SAM format. Then using DirectoryEntry, DirectorySearcher and other AD API's iterate through all the joined domain of the machine and find if the user is in any of the domain we iterate.
Find all domains of the machine ex1 and ex2
Here is How to get domain user information from Active Directory in C#
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With