我在C#中使用ASP.net,並且對Active Directory有一點點想法。我已經給了一個任務來編寫一個程序,步驟如下:找出AD中的某個組是否位於通訊組中?
ASP.net應用程序被賦予用戶的用戶名。
應用程序應該使用給定的用戶名來查詢用戶的所有組。
然後應用程序應該在兩個單獨的列表中顯示這些組,其中一個由通訊組和其他列表組成,另一個列表中包含其他組。
現在,查詢所有組很容易。但是,我如何檢查該組是否在分發組中呢?
我還沒有得到更多的信息。
任何屬性或我可以檢查的東西?
我在C#中使用ASP.net,並且對Active Directory有一點點想法。我已經給了一個任務來編寫一個程序,步驟如下:找出AD中的某個組是否位於通訊組中?
ASP.net應用程序被賦予用戶的用戶名。
應用程序應該使用給定的用戶名來查詢用戶的所有組。
然後應用程序應該在兩個單獨的列表中顯示這些組,其中一個由通訊組和其他列表組成,另一個列表中包含其他組。
現在,查詢所有組很容易。但是,我如何檢查該組是否在分發組中呢?
我還沒有得到更多的信息。
任何屬性或我可以檢查的東西?
您可以從名爲Groupe-Type(最後一行)的屬性中檢索此信息。
(0x00000001) : Specifies a group that is created by the system.
(0x00000002) : Specifies a group with global scope.
(0x00000004) : Specifies a group with domain local scope.
(0x00000008) : Specifies a group with universal scope.
(0x00000010) : Specifies an APP_BASIC group for Windows Server Authorization Manager.
(0x00000020) : Specifies an APP_QUERY group fir Windows Server Authorization Manager.
(0x80000000) :Specifies a security group. If this flag is not set, then the group is a distribution group.
您可以this answer或在this other one不同的方式鈕找到中檢索組用戶所屬。
你可以找到here如何檢索用戶。
由於.NET 3.5及更高版本,您應該檢查System.DirectoryServices.AccountManagement
(S.DS.AM)命名空間。在這裏閱讀全部內容:
基本上,你可以定義域範圍內,並可以輕鬆地查找用戶和/或組AD:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// get all roles for that user
var roles = user.GetGroups();
// set up two lists for each type of groups
List<GroupPrincipal> securityGroups = new List<GroupPrincipal>();
List<GroupPrincipal> distributionGroups = new List<GroupPrincipal>();
// iterate over groups found
foreach (Principal p in roles)
{
// cast to GroupPrincipal
GroupPrincipal gp = (p as GroupPrincipal);
if (gp != null)
{
// check whether it's a security group or a distribution group
if (gp.IsSecurityGroup)
securityGroups.Add(gp);
else
distributionGroups.Add(gp);
}
}
}
的新的S.DS.AM可以很容易地與AD中的用戶和羣組玩耍!
非常感謝。這似乎工作。它給出了兩個清單,但管理層聲稱這兩個清單是錯誤的!這意味着某些通訊組在安全組列表中。也許他們錯了。無論如何,非常感謝你。順便說一下,編譯時我遇到了這個奇怪的錯誤:'不能隱式轉換類型'布爾'?到'布爾'。存在明確的轉換(您是否缺少演員?)'。當投到布爾時,這是沒問題的。但是這個數據類型'布爾'到底是什麼? ???從來沒有聽說過! – PPGoodMan
@PPGoodMan:這是一個**可爲空的布爾**,這意味着它可以是NULL,真或假。 –
此代碼將檢索您的所有啓用電子郵件的羣組,無論它是安全還是分發羣組。 (看到你對marc_s的回答的評論,我猜這實際上是你的經理正在尋找的東西)。
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
Principal prototype = new GroupPrincipal(ctx);
PrincipalSearcher searcher = new PrincipalSearcher(prototype);
List<string> groupNames = new List<string>();
PropertyValueCollection email;
foreach (var gp in searcher.FindAll()) using (gp)
{
GroupPrincipal group = gp as GroupPrincipal;
using (DirectoryEntry groupEntry = ((DirectoryEntry)group.GetUnderlyingObject())
{
email = groupEntry.Properties["mail"];
if (email.Value != null)
{
groupNames.Add(group.Name);
}
}
}
}
我還不確定您的答案。因爲我還沒有弄清楚到底發生了什麼!但我不問你是什麼意思,因爲自從我有時間以後我想弄清楚自己。非常感謝。 – PPGoodMan