Active Directory本地计算机帐户管理 - C#

时间:2009-09-11 10:23:08

标签: c# .net active-directory

我发布了一个关于LDAP帐户管理的问题,但在探索之后,这不是我想要的。我已经设法找到两种在一台机器上创建用户的方法,我发现一种方法比另一种方式更整洁,但是,我不确定如何将第一个选项完全转换为第二个选项。

这是我的第一个解决方案:

        Process MyProc = new Process();
        MyProc.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
        MyProc.StartInfo.FileName = "net.exe";
        MyProc.StartInfo.UseShellExecute = false;
        MyProc.StartInfo.RedirectStandardError = true;
        MyProc.StartInfo.RedirectStandardInput = true;
        MyProc.StartInfo.RedirectStandardOutput = true;
        MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        MyProc.StartInfo.Arguments = string.Format(@" user {0} {1} /ADD /ACTIVE:YES /EXPIRES:NEVER /FULLNAME:{0}"" /PASSWORDCHG:NO /PASSWORDREQ:YES", username, password);

        MyProc.Start();
        MyProc.WaitForExit();
        int exit = MyProc.ExitCode;

        MyProc.Close();

        return exit == 0;

这是我的第二个(优先)解决方案:

        DirectoryEntry root = GetDELocalRoot();
        DirectoryEntry user = root.Children.Add(username, "user");
        //TODO: Always Active
        //TODO: Never Expires
        //TODO: No Password Change
        //TODO: Password Required
        user.Properties["description"].Value = "Account for running the MicaService and handling updates.";
        user.Invoke("SetPassword", new object[] { password });

        user.CommitChanges();
        user.Close();

我想映射我的TODO中的所有设置:从第一个解决方案到我的第二个整体解决方案。

我也尝试过以下一行:

user.Properties["userAccountControl"].Value = ADS_USER_FLAG.ADS_UF_NORMAL_ACCOUNT | ADS_USER_FLAG.ADS_UF_PASSWD_CANT_CHANGE | ADS_USER_FLAG.ADS_UF_DONT_EXPIRE_PASSWD;

但这不起作用,因为缓存中不存在该属性。

注意:GetDELocalRoot()=返回新的DirectoryEntry(“WinNT://”+ Environment.MachineName);

感谢您的任何意见!

此致

的Tris

1 个答案:

答案 0 :(得分:1)

查看我的朋友Richard Mueller's web site,其中有很多有用的信息和参考资料,介绍这两个提供商 - 本地计算机帐户的WinNT与网络帐户的LDAP - 必须提供。

WinNT提供商还提供了Excel sheeet with all attributes - 它比LDAP提供商提供的要少得多,所以我不确定你是否能够设置你正在寻找的所有属性

马克