[英]Reading Local Group Policy / Active Directory Settings
我正在編寫一個C#程序,它將根據Windows組策略設置“密碼必須滿足復雜性要求”來強制設置密碼復雜性。 具體來說,如果在本地計算機(如果它不是域的一部分)上或通過域安全策略(對於域成員)將該策略設置為“已啟用”,則我的軟件需要為其自身的內部安全性強制執行復雜的密碼。
問題是我不知道如何讀取該GPO設置。 Google搜索表明,我可以使用以下兩個API之一讀取GPO設置:.NET Framework中的System.DirectoryServices庫和Windows Management Instrumentation(WMI),但是到目前為止,我還沒有取得任何成功。
任何見解都會有所幫助。
似乎沒有針對此任務的文檔化API,無論是托管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;
..it不會告訴您是否啟用了密碼復雜性策略。
因此,我訴諸解析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
您可以使用策略結果集(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
我碰到了您這個Microsoft論壇的答案http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/f3f5a61f-2ab9-459e-a1ee-c187465198e0
希望這對將來遇到這個問題的人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.