2009年3月16日 星期一

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

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;
//叫用 Group 的 Members
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;
}

沒有留言: