2010-09-20 60 views
1

我有一段代碼可以創建Windows用戶。一切都很好,用戶確實創建。但是,當我試圖鎖定LoadUserProfile時,操作失敗,因爲用戶不在Users組中。現在,我知道如何以實用的方式將用戶添加到Users組,但我不想硬編碼組名「Users」,因爲它可能會根據語言環境而更改。有沒有辦法將用戶添加到默認的用戶組(順便說一下,runas user/add命令已經這樣做了)?默認情況下,如何向用戶組添加新的Windows用戶


DirectoryEntry dirEntry = new DirectoryEntry("WinNT://" + domain); 
DirectoryEntries entries = dirEntry.Children; 
DirectoryEntry user = entries.Add(username, "User"); 
user.Properties["FullName"].Add("Dr Zoidberg"); 
user.Invoke("SetPassword", password); 
user.CommitChanges(); 

回答

2

Windows和Active Directory有許多「衆所周知的SID」,它們是內置帳戶和組的安全標識符。您可以使用衆所周知的SID綁定到Users組,因爲不管區域設置如何,它都不會更改。管理員甚至可以將用戶組重命名爲其他內容,但SID將保持不變。

衆所周知的SID在System.Security.Principal.WellKnownSidType

列舉詳情請參閱http://msdn.microsoft.com/en-us/library/system.security.principal.wellknownsidtype.aspx

+0

謝謝,就是這樣! – kateroh 2010-09-21 03:26:35

相關問題