Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know whether an user account exists

  1. How do I know if an user account exists on my Windows OS (Vista)? I need this information from a stand alone machine that hasn't joined any domain.

  2. I want to know whether an user is a part of a group, for example is a user 'admin' part of 'Administrators' group or not?

like image 489
satya Avatar asked Jan 11 '10 16:01

satya


People also ask

How do I know if an AD user exists?

If you just run Test-ADUser -Username $Username, it will return the user properties AND true if the user exists and False if it does not.

How do you check whether a user exists in Linux?

Using the id Linux Command Well, it displays the user information of whoever we type after it. Above, the id command displays information related to the user peter because they exist. For instance, Linux assigns every user an ID unique only to them, in this case, the uid (USER ID) of 1000.

How can I see current users?

Type whoami and press Enter. Your current user name will be displayed.

How do you check if a group exists in Linux?

To view all groups present on the system simply open the /etc/group file. Each line in this file represents information for one group. Another option is to use the getent command which displays entries from databases configured in /etc/nsswitch.


2 Answers

You can work out if a local account exists through the System.Security.Principal namespace using the following code.

bool AccountExists(string name)
{
    bool bRet = false;

    try
    {
        NTAccount acct = new NTAccount(name);
        SecurityIdentifier id = (SecurityIdentifier)acct.Translate(typeof(SecurityIdentifier));

        bRet = id.IsAccountSid();
    }
    catch (IdentityNotMappedException)
    {
        /* Invalid user account */
    }

    return bRet;
}

Now getting group membership is slightly harder, you can easily do it for the current user using the WindowsPrinciple.IsInRole method (creating a principle from the WindowsIdentify.GetCurrent() method).

As pointed out I don't think there is a way of getting anything else without resorting to PInvoke or WMI. So here is a bit of code to check group membership with WMI.

bool IsUserInGroup(string name, string group)
{
    bool bRet = false;
    ObjectQuery query = new ObjectQuery(String.Format("SELECT * FROM Win32_UserAccount WHERE Name='{0}' AND LocalAccount=True", name));
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    ManagementObjectCollection objs = searcher.Get();

    foreach (ManagementObject o in objs)
    {
        ManagementObjectCollection coll = o.GetRelated("Win32_Group");
        foreach (ManagementObject g in coll)
        {
            bool local = (bool)g["LocalAccount"];
            string groupName = (string)g["Name"];

            if (local && groupName.Equals(group, StringComparison.InvariantCultureIgnoreCase))
            {
                bRet = true;
                break;
            }
        }
    }           

    return bRet;
}
like image 136
tyranid Avatar answered Oct 05 '22 23:10

tyranid


I have tried the following code and is working fine for me..

    public bool IsUserMemberOfGroup(string userName, string groupName)
    {
        bool ret = false;

        try
        {
            DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
            DirectoryEntry userGroup = localMachine.Children.Find(groupName, "group");

            object members = userGroup.Invoke("members", null);
            foreach (object groupMember in (IEnumerable)members)
            {
                DirectoryEntry member = new DirectoryEntry(groupMember);
                if (member.Name.Equals(userName, StringComparison.CurrentCultureIgnoreCase))
                {
                    ret = true;
                   break;
                }
            }
        }
        catch (Exception ex)
        {
            ret = false;
        }
        return ret;
    }
like image 31
satya Avatar answered Oct 06 '22 01:10

satya