2009年3月16日 星期一

ADSI技術文章-使用WinNT Provider取得User的Groups

問題說明:
如果我們想使用WinNT Provider,取得user的群組清單,應該如何做呢?
下面範例會傳回一組群組名稱和描述的Dictionary資料:

public Dictionary GetWinNtUserGroups(string userName, string uName, string pwd)
{
Dictionary rv = new Dictionary();
string adsPath = string.Format("WinNT://{0}/{1},user", Environment.MachineName, userName);
if (DirectoryEntry.Exists(adsPath) == true)
{
DirectoryEntry user = new DirectoryEntry(adsPath, uName, pwd);
IADsGroup ig;
//叫用 User 的 Groups
object groups = user.Invoke("Groups", null);
DirectoryEntry gEntry;
foreach (object go in (IEnumerable)groups)
{
gEntry = new DirectoryEntry(go);
if (gEntry.SchemaClassName.ToLower() == "group")
{
ig = (IADsGroup)gEntry.NativeObject;
rv.Add(ig.Name, ig.Description);
}
}
}
return rv;
}
*使用IADsGroup的COM介面,可以輕鬆取得group的屬性。
*使用IADsGroup的COM介面,須參考ActiveDs.dll。

沒有留言: