如何知道用户帐号是否存在

发布于 2024-08-18 00:18:06 字数 144 浏览 5 评论 0原文

  1. 我如何知道我的 Windows 操作系统 (Vista) 上是否存在用户帐户?我需要来自未加入任何域的独立计算机的此信息。

  2. 我想知道某个用户是否属于某个组,例如用户“admin”是否属于“管理员”组?

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末 2024-08-25 00:18:06

您可以通过 确定本地帐户是否存在System.Security.Principal 命名空间使用以下代码。

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;
}

现在获取组成员身份稍微困难一些,您可以使用 WindowsPrinciple.IsInRole 方法(从 WindowsIdentify.GetCurrent() 方法)。

正如所指出的,我认为没有办法在不诉诸 PInvoke 或 WMI 的情况下获得其他任何东西。下面是一些使用 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;
}

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;
}
千里故人稀 2024-08-25 00:18:06

我已经尝试过以下代码并且对我来说工作正常..

    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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文