I want to be able to check if the user running the current thread is:
There are some cavaets:
In other words, i want:
public static Boolean IsLocalUser()
{
//code here
}
What follows is extraneous bonus information. It adds nothing to the question, but is here solely to make the question longer.
Check if machine is domain joined using NetGetJoinInformation
I can use NetGetJoinInformation
to check if the local machine is joined to a domain:
NetGetJoinInformation(null, ref domain, ref joinStatus);
Returns:
domain
contains name of workgroup (e.g. WORKGROUP
)domain
contains the legacy NetBIOS name of the domain (e.g. STACKOVERFLOW
)Check if machine is domain joined using DsRoleGetPrimaryDomainInformation
I can use DsRoleGetPrimaryDomainInformation
to check if the local machine is joined to a domain:
DsRoleGetPrimaryDomainInformation(null,
DsRolePrimaryDomainInfoBasic,
DSROLE_PRIMARY_DOMAIN_INFO_BASIC);
Returns a DSROLE_PRIMARY_DOMAIN_INFO_BASIC
structure:
MachineRole
DsRole_RoleStandaloneWorkstation
: the machine is not domain joinedDsRole_RoleStandaloneServer
: the machine is not domain joinedDsRole_RoleMemberWorkstation
: the machine is domain joinedDsRole_RoleMemberServer
: the machine is domain joinedDsRole_RoleBackupDomainController
: the machine is domain joinedDsRole_RolePrimaryDomainController
: the machine is domain joinedDomainNameDns
: returns the DNS Domain name (e.g. stackoverflow.com
) (optional)
DomainNameFlat
: returns the legacy NetBIOS domain name (e.g. STACKOVERFLOW
)But, again, telling me name name of the domain that the machine is joined to doesn't help when the user is not a domain user, or the user is from a different domain
Getting the name of the current user using GetUserName
Windows does provide a GetUserName
function:
GetUserName(buffer, Length(buffer));
returns the name of the user, e.g. lsimpson
Getting the name of the current user using GetUserNameEx
Windows does provide a GetUserNameEx
function, that allows you to return the username in different formats:
CN=John Smith,OU=Stackoverflow Users,DC=stackoverflow,DC=com
STACKOVERFLOW\jsmith
John
{C1AF1DE6-363D-42A7-BB0D-9D1EDDC44B81}
stackoverflow.com/Stackoverfow Users/John Smith
[email protected]
stackoverflow.com/Stackoverflow Users/John Smith
STACKOVERFLOW.COM\jsmith
also
HYDROGEN\john
KB11154: How to retrieve current user and domain name
Microsoft has a knowledge base article:
How to retrieve current user and domain names on Windows NT, Windows 2000, or Windows XP
it involves:
OpenThreadToken
OpenProcessToken
(if there is no thread token)
GetTokenInformation
- to get a TOKEN_USER
LookupAccountSid
- to get a username and domain name from a TOKEN_USER
In the end it returns:
jsmith
HYDROGEN
, STACKOVERFLOW
Good afternoon!
I looked into some of the other solutions posted for this problem, but most of them do not take into account the caveats that you listed. Most of the relevant solutions are from the following post:
Check if user is a Domain User or Local User
Of these solutions, the following seems to be the most in line with what you proposed:
bool IsLocalUser(string accountName)
{
var domainContext = new PrincipalContext(ContextType.Machine);
return Principal.FindByIdentity(domainContext, accountName) != null;
}
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