8

I want to be able to check giving a username if that user is a Domain User or a Local User (using .NET preferable) of the machine but could find much on this on the net

public static Boolean isLocalUser (string name)
{
//code here
}

EDIT for example you are given me.user as a string

2
  • @MicahArmantrout was looking up some .net Property can see any thing to check this Oct 3, 2012 at 14:20
  • 1
    I don't know, why this question is sadly marked as dupe, because checking if an user exists on a domain is different to checking for a local user first. Checking a domain first can cause a long timeout for seconds before any result will be returned. On the other side, if you just check for Environment.UserDomainName, it will return the local hostname, if the user was logged in to the machine locally, which can be directly grabbed from Environment.MachineName. So if you just check on that the UserDomainName and the hostname is matching, the user is a local user. Quite simple and fast..
    – SiL3NC3
    Sep 2, 2021 at 12:24

4 Answers 4

4
public bool DoesUserExist(string userName)
{
    bool exists = false;
    try
    {
    using (var domainContext = new PrincipalContext(ContextType.Domain, "DOMAIN"))
    {
        using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName))
        {
            exists = true;
        }
      }
    }
    catch(exception ex)
    {
      //Exception could occur if machine is not on a domain
    } 
    using (var domainContext = new PrincipalContext(ContextType.Machine))
    {
        using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userName))
        {
            exists = true;
        }
    }
   return exists;
}

Source: Check UserID exists in Active Directory using C#

5
  • Are we sure this code won't except in the first 'using' block when executed on a machine that is not a member of a domain?
    – David W
    Oct 3, 2012 at 16:31
  • it very well could I am not 100% sure Oct 3, 2012 at 17:24
  • Well, I would respectfully suggest that should be specifically checked. The OP specifically requested a function to check if the user "is local," and this code won't do much good if it excepts precisely in that circumstance. I would be glad to fire up a quick test myself, but none of the machines here are standalone boxes.
    – David W
    Oct 3, 2012 at 17:41
  • Thanks. If I can find a standalone box to check the actual exception thrown (if any), I'll make a note to myself to post it here.
    – David W
    Oct 3, 2012 at 17:47
  • Wow, this is so wrong.
    – user1228
    May 19, 2015 at 20:01
4

David is on the money, to check if the current user is part of the domain, you can check the Environment.UserDomainName and compare this with the current user.

Bit more info at MSDN

1
  • 1
    I don't know, why this question is sadly marked as dupe, because checking if an user exists on a domain is different to checking for a local user first. Checking a domain first can cause a long timeout for seconds before any result will be returned. On the other side, if you just check for Environment.UserDomainName, it will return the local hostname, if the user was logged in to the machine locally, which can be directly grabbed from Environment.MachineName. So if you just check on that the UserDomainName and the hostname is matching, the user is a local user. Quite simple and fast..
    – SiL3NC3
    Sep 2, 2021 at 12:18
4

For me this has worked quite well so far. (left usings away).

bool IsLocalUser(string accountName)
{
  var domainContext = new PrincipalContext(ContextType.Machine);
  return Principal.FindByIdentity(domainContext, accountName) != null;
}

But be aware that this will not recognize the "System" account. We check that separately because it's always a local account.

2

A local user will have the account name prefixed with the machine name. A domain user's account name will be prefixed with its originating domain. If the machine name and the account prefix name don't match, or if the account name prefix matches the local machine, it's generally safe to assume its a local account.

2
  • but if you are given a string like "joe.bloggs" then how do you get the full username give that string Oct 3, 2012 at 14:17
  • 2
    If the caller provides an arbitrary string, then there's no way to resolve unless you first interrogate the local account database, then the domain, and even then it is up to the problem domain to determine which should trump the other. The DoesUserExist method provided in the accepted answer assumes that a domain user should trump a machine user.
    – David W
    Oct 3, 2012 at 16:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.