示例#1
0
        //http://support.microsoft.com/kb/306273
        //http://www.gotdotnet.ru/blogs/sergeyhomyuk/10326/
        public static String CreateLocalUser(string login, string fullName, string password, TServer Server)
        {
            try
            {
                DirectoryEntry root = new DirectoryEntry(string.Format("WinNT://{0},computer", Server.IP));
                using (DirectoryEntry user = root.Children.Add(login, "user"))
                {

                    user.Properties["FullName"].Value = fullName;
                    user.Properties["Description"].Value = DateTime.Now.ToString();
                    user.Invoke("SetPassword", new object[] { password });
                    user.CommitChanges();

                    string UserPath = user.Path.ToString().Replace(Server.IP, Server.Name);

                    DirectoryEntry grp = root.Children.Find("Спутник ОТЦ3 Челябинск", "group");
                    if (grp != null) { grp.Invoke("Add", new object[] { UserPath }); }
                    return String.Format("Пользователь {0} создан на сервере {1}.", login, Server);
                }
            }
            catch (COMException e)
            {
                return String.Format("Пользователь {0} не создан на сервере {1}. Ошибка: '{2}'", login, Server,e.Message);
            }
        }
示例#2
0
        //http://support.microsoft.com/kb/306273
        //http://www.gotdotnet.ru/blogs/sergeyhomyuk/10326/
        public static String CreateLocalUser(string login, string fullName, string password, TServer Server)
        {
            try
            {
                DirectoryEntry root = new DirectoryEntry(string.Format("WinNT://{0},computer", Server.IP));
                using (DirectoryEntry user = root.Children.Add(login, "user"))
                {
                    user.Properties["FullName"].Value    = fullName;
                    user.Properties["Description"].Value = DateTime.Now.ToString();
                    user.Invoke("SetPassword", new object[] { password });
                    user.CommitChanges();

                    string UserPath = user.Path.ToString().Replace(Server.IP, Server.Name);


                    DirectoryEntry grp = root.Children.Find("Спутник ОТЦ3 Челябинск", "group");
                    if (grp != null)
                    {
                        grp.Invoke("Add", new object[] { UserPath });
                    }
                    return(String.Format("Пользователь {0} создан на сервере {1}.", login, Server));
                }
            }
            catch (COMException e)
            {
                return(String.Format("Пользователь {0} не создан на сервере {1}. Ошибка: '{2}'", login, Server, e.Message));
            }
        }