-->

读本地组策略/ Active Directory设置(Reading Local Group Pol

2019-07-20 07:50发布

我正在写一个C#程序,将按照Windows组策略设置“密码必须符合复杂性要求”强制密码复杂性。 具体而言,如果该策略设置为启用在本地机器上(如果它不是域的一部分),或者通过域安全策略(域成员),那么我的软件需要强制执行其自己的内部安全一个复杂的密码。

问题是,我无法弄清楚如何读取GPO设置。 谷歌搜索表明,我可以用这两个API的一个阅读GPO设置:图书馆的System.DirectoryServices .NET Framework中,和Windows管理规范(WMI),但我还没有成功为止。

任何见解将是有益的。

Answer 1:

似乎没有要完成这个任务,文档化的API,管理或以其他方式。

管理的尝试

我尝试使用托管路线System.Management组件:

        ConnectionOptions options = new ConnectionOptions();
        ManagementScope scope = new ManagementScope(@"\\.\root\RSOP\Computer", options);

        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM RSOP_SecuritySettingBoolean"));
        foreach(ManagementObject o in searcher.Get())
        {
            Console.WriteLine("Key Name: {0}", o["KeyName"]);
            Console.WriteLine("Precedence: {0}", o["Precedence"]);
            Console.WriteLine("Setting: {0}", o["Setting"]);
        }

然而,这将不返回结果。 它不似乎是一个权限问题作为提供用户名/密码对, ConnectionOptions一个异常结果告诉你,在本地连接的时候可以不指定用户名。

不受管理的尝试

我看着NetUserModalsGet 。 虽然这将返回密码设置的一些信息:

typedef struct _USER_MODALS_INFO_0 {
  DWORD usrmod0_min_passwd_len;
  DWORD usrmod0_max_passwd_age;
  DWORD usrmod0_min_passwd_age;
  DWORD usrmod0_force_logoff;
  DWORD usrmod0_password_hist_len;
} USER_MODALS_INFO_0, *PUSER_MODALS_INFO_0, *LPUSER_MODALS_INFO_0;

..它不会让告诉我们,如果密码复杂性启用政策。

工具输出刮痧“成功”

于是我使出解析secedit.exe输出。

    public static bool PasswordComplexityPolicy()
    {
        var tempFile = Path.GetTempFileName();

        Process p = new Process();
        p.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\secedit.exe");
        p.StartInfo.Arguments = String.Format(@"/export /cfg ""{0}"" /quiet", tempFile);
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.UseShellExecute = false;
        p.Start();
        p.WaitForExit();

        var file = IniFile.Load(tempFile);

        IniSection systemAccess = null;
        var passwordComplexityString = "";
        var passwordComplexity = 0;

        return file.Sections.TryGetValue("System Access", out systemAccess)
            && systemAccess.TryGetValue("PasswordComplexity", out passwordComplexityString)
            && Int32.TryParse(passwordComplexityString, out passwordComplexity)
            && passwordComplexity == 1;
    }

这里全码: http://gist.github.com/421802



Answer 2:

您可以使用的结果集的策略(RSOP)工具。 例如,这里有一个VBScript(从解禁这里 ),它会告诉你你需要知道的东西。 它应该是足够简单翻译成C#这一点。

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\rsop\computer")
Set colItems = objWMIService.ExecQuery _
    ("Select * from RSOP_SecuritySettingBoolean")
For Each objItem in colItems
    Wscript.Echo "Key Name: " & objItem.KeyName
    Wscript.Echo "Precedence: " & objItem.Precedence
    Wscript.Echo "Setting: " & objItem.Setting
    Wscript.Echo
Next


Answer 3:

我碰到你这个微软论坛答案出来http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/f3f5a61f-2ab9-459e-a1ee-c187465198e0

希望这可以帮助别人谁在未来遇到这个问题。



文章来源: Reading Local Group Policy / Active Directory Settings