Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify user logged in to domain or locally

When my software is run my software most be get type of login of the windows user. Login by domain or local. I need to get type of login of the user in windows.

I think these APIs can help me:

GetComputerNameEx( ComputerNameDnsDomain, domainNameBuf, &bufSize );    

or

GetUserName(sBuffer.GetBuffer(dwUsernameSize), &dwUsernameSize);

or

LookupAccountName(NULL, 
                  sUsername,
                  (PSID)pSid,
                  &dwSidSize,
                  sBuffer.GetBuffer(dwDomainNameSize), 
                  &dwDomainNameSize,
                  (PSID_NAME_USE)&sidType);

or

nStatus = NetWkstaUserGetInfo(NULL,dwLevel,(LPBYTE *)&pBuf);
like image 304
hessamini Avatar asked Oct 20 '25 11:10

hessamini


2 Answers

Use LsaQueryInformationPolicy with PolicyDnsDomainInformation to retrieve the SID for the computer's primary domain. If the Sid member is NULL, the computer is not joined to a domain and you can assume the user is logged into a local account.

Otherwise, use GetTokenInformation with TokenUser to retrieve the SID for the current user, then use GetWindowsAccountDomainSid to extract the domain part of the user's SID.

Compare the primary domain's SID to the user's domain SID using EqualSid. If the SIDs are equal, the user is logged into a domain account; otherwise, the user is logged into a local account.

like image 91
Harry Johnston Avatar answered Oct 23 '25 00:10

Harry Johnston


You can do it in a simple way, why don't you just use GetEnvironmentVariable to retreive USERDOMAIN and then compare with COMPUTERNAME. If they are identical then the user is logged localy, in the other way the user is logged using the domain.

you also got the command line WHOAMI.EXE that gives you the information and the one I use is retreiving the WMI object win32_computersystem.

like image 32
JPBlanc Avatar answered Oct 22 '25 23:10

JPBlanc