Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out that my Windows user is a domain user?

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());
like image 396
M.Azad Avatar asked Oct 08 '22 14:10

M.Azad


1 Answers

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#

like image 68
Abhijit-K Avatar answered Oct 15 '22 08:10

Abhijit-K