【问题标题】:Adding a local user to a local group in C#在 C# 中将本地用户添加到本地组
【发布时间】:2012-06-12 15:08:57
【问题描述】:

我可以很好地添加一个用户,但是我不能将它添加到本地组。我收到此错误:-

无法将成员添加到本地组或从本地组中删除,因为 该成员不存在。

这是我正在使用的代码。我做错了什么?这只是本地机器,我绝对有权这样做,并且该组肯定存在。

        try
        {
            using (DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://" + serverName))
            {
                DirectoryEntries entries = hostMachineDirectory.Children;

                foreach (DirectoryEntry entry in entries)
                {
                    if (entry.Name.Equals(userName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        // Update password
                        entry.Invoke("SetPassword", password);
                        entry.CommitChanges();
                        return true;
                    }
                }

                DirectoryEntry obUser = entries.Add(userName, "User");
                obUser.Properties["FullName"].Add("Used to allow users to login to Horizon. User created programmatically.");
                obUser.Invoke("SetPassword", password);
                obUser.Invoke("Put", new object[] {
                "UserFlags",
                0x10000
                });

                obUser.CommitChanges();

                foreach (string group in groups)
                {
                    DirectoryEntry grp = hostMachineDirectory.Children.Find(group, "group");
                    if (grp != null) { grp.Invoke("Add", new object[] { obUser.Path.ToString() }); }

                }
                return true;
            }
        }
        catch (Exception ex)
        {
            returnMessage = ex.InnerException.Message;
            return false;
        }

【问题讨论】:

    标签: c# directoryentry


    【解决方案1】:

    我很久以前写了一些代码,它采用了与您不同的方法,但据我所知,工作正常(只要没有人向我报告过问题!)。对你有用吗?

        /// <summary>
        /// Adds the supplied user into the (local) group
        /// </summary>
        /// <param name="userName">the full username (including domain)</param>
        /// <param name="groupName">the name of the group</param>
        /// <returns>true on success; 
        /// false if the group does not exist, or if the user is already in the group, or if the user cannont be added to the group</returns>
        public static bool AddUserToLocalGroup(string userName, string groupName)
        {
            DirectoryEntry userGroup = null;
    
            try
            {
                string groupPath = String.Format(CultureInfo.CurrentUICulture, "WinNT://{0}/{1},group", Environment.MachineName, groupName);
                userGroup = new DirectoryEntry(groupPath);
    
                if ((null == userGroup) || (true == String.IsNullOrEmpty(userGroup.SchemaClassName)) || (0 != String.Compare(userGroup.SchemaClassName, "group", true, CultureInfo.CurrentUICulture)))
                    return false;
    
                String userPath = String.Format(CultureInfo.CurrentUICulture, "WinNT://{0},user", userName);
                userGroup.Invoke("Add", new object[] { userPath });
                userGroup.CommitChanges();
    
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (null != userGroup) userGroup.Dispose();
            }
        }
    

    【讨论】:

    • 好。介意将其标记为答案吗?我可以使用这些积分,所以每个人都认为我比我实际上更有知识! P
    • 如果 userName 是服务帐户而不是本地用户(例如 NT AUTHORITY\IUSR)怎么办?它对我不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多