6

Is there a way to determine (.NET preferably) if the current user is a domain user account or local user account?

Ahead of time - I don not know the domain name this is running on so I can't just look for DOMAIN\Username v COMPUTER\Username.

Part of the answer could be determining the DOMAIN or COMPUTER name from code.

[Edit] Expanding on Asher's answer a code fragment would be

private bool isCurrentUserLocalUser()
{
    return Environment.MachineName == Environment.UserDomainName;
}
Ryan
  • 23,871
  • 24
  • 86
  • 132
  • I changed the code to use case-insensitive comparison, because Environment.UserDomainName returns lowercase names in some environments ('some-PC') while Environment.MachineName is uppercase ('SOME-PC') – mistika Mar 06 '17 at 18:31

3 Answers3

2

See that post and check if it's answering your question

how-do-i-detect-if-my-program-runs-in-an-active-directory-environment

Community
  • 1
  • 1
vIceBerg
  • 4,197
  • 5
  • 40
  • 53
  • Not exactly the same (infact that q is not to clear on exactly what is is asking - is it an ad environment/does the current user have an ad account/is current user logged on via ad etc) but helpful anyway - thanks – Ryan Nov 21 '08 at 21:40
1

you can use Environment.UserDomainName the MSDN documentation explains the exact behavior of this property in each case (domain/local account)

You can use this property in conjunction with other properties like Environment.MachineName to figure out what is the type of the user account being used.

Note that domain account is not necessarily Active Directory (can be that Novell Netware stuff)

Asher
  • 1,867
  • 15
  • 24
  • 1
    THsi doesn't work because UserDomainName will give you workgroup name as well, which pretty often is the same as machine name. – Ivan G. Jan 12 '12 at 12:25
0

You could look at the full username, which is either <domain name>\<username> or <machine name>\<username> for domain and local accounts respectively. If the first part matches the domain name, it's obviously a domain account and the opposite holds true.

DCNYAM
  • 11,966
  • 8
  • 53
  • 70
  • How do I programatically find out what the domain name is - how do I know that its a domain name and not that particular computers netbios name? – Ryan Nov 21 '08 at 20:58