2012-09-17 36 views
0

我有一個在C#中使用Windows身份驗證的Web應用程序,目前我將用戶分配給各個角色。如何爲Active Directory組中的用戶設置訪問

例如在應用程序的每一頁,我檢查

if(Roles.IsUserInRole(AU\UserName, "PageAccessRole")) 

正如我需要推出本週應用到整個團隊(和最終整個公司),我需要用戶的廣告組有超過3000個PPL所以我不打算手工做!

作爲一個新手到ASP.NET(和一般的編程),我真的不知道很多關於設置廣告組(例如,如何從我的應用等方面獲得訪問AD組?)

我會很感激,如果任何人都可以指向我在正確的方向......我一直在讀全部LDAPSystem.DirectoryServices.AccountManagement等,但我只是越來越困惑。

到目前爲止,我有這個在我的web.config

<authentication mode="Windows"> 
    </authentication> 
    <authorization> 
       <allow roles="AU\Active Directory Group Name"/> 
    <deny users="?"/> 
    </authorization> 

    <roleManager enabled="true" > 
    <providers> 
    <clear/> 
    <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> 
     </providers> 

而且我已經啓用了Windows身份驗證和禁用匿名在IIS服務器。

請請幫忙!!

+0

我懷疑這個線程(http://stackoverflow.com/questions/2188954/see-if-user-is-part-of-active-directory-group-in-c-sharp-asp-net )會很有用,但我不知道如何將代碼合併到我的應用程序中... –

+0

是否要添加新組並將人員添加到組中? – RL89

+0

不,我想使用公司現有的Active Directory組! –

回答

1

解決方案: -

這是如何從一個OU在AD

DataTable dt = new DataTable(); 
dt.Columns.Add("groups"); 
DirectoryEntry rootDSE = null; 

假設我想從我處OU提取記錄取出組。現在的路徑是這樣的

部 - >>用戶

DC這裏是域控制器的名字,對我來說,這是Corp.Local
這樣就可以從您的公司獲取羣組

if (department != "") 
{ 
    rootDSE = new DirectoryEntry(
    "LDAP://OU=" + department + ",OU=Users,dc=corp,dc=local", username, password); 
} 
else 
{ 
    rootDSE = new DirectoryEntry(
     "LDAP://OU=Users,OU=" + ou + ",dc=corp,dc=local", username, password); 
} 
DirectorySearcher ouSearch = new DirectorySearcher(rootDSE); 
ouSearch.PageSize = 1001; 
ouSearch.Filter = "(objectClass=group)"; 
ouSearch.SearchScope = SearchScope.Subtree; 
ouSearch.PropertiesToLoad.Add("name"); 
SearchResultCollection allOUS = ouSearch.FindAll(); 
foreach (SearchResult oneResult in allOUS) 
{ 
    dt.Rows.Add(oneResult.Properties["name"][0].ToString()); 
} 
rootDSE.Dispose(); 
return dt; 

現在如何將用戶添加到羣組中。

這是一個單用戶示例,您可以通過循環用戶以類似的方式執行此操作。

PrincipalContext pr = new PrincipalContext(ContextType.Domain, 
    "corp.local", "dc=corp,dc=local", username, password); 
GroupPrincipal group = GroupPrincipal.FindByIdentity(pr, groupName);//Looking for the Group in AD Server 

if (group == null) 
    { 
    //Throw Exception 
    } 

UserPrincipal user = UserPrincipal.FindByIdentity(pr, userName);//Looking for the User in AD Server 

if (user.IsMemberOf(group))//If Group is already added to the user 
    { 
     //I have Put it into If else condition because in case you want to Remove Groups from that User you can write your Logic here. 

    //Do Nothing, Because the group is already added to the user 
    } 
else// Group not found in the Current user,Add it 
    { 
     if (user != null & group != null) 
     { 
     group.Members.Add(user); 
     group.Save(); 
     done = user.IsMemberOf(group);//You can confirm it from here 
     } 
    } 
    pr.Dispose(); 
    return done; 
+0

另一個問題,如果你能幫助我!我只使用了代碼的第一部分(即檢查是否登錄特定AD組的用戶部分)。現在,如果用戶是該AD組的一部分,我想讓他們對應用程序擁有某些安全權限。我創建了一個虛擬用戶,分配了我希望AD組的每個人都有的角色。我如何允許用戶從僞用戶「繼承」安全權限? –

+0

基本上,我只是希望AD組的那些部分具有某些安全權限(而不是將每個用戶添加到單個安全權限)。 –

+0

我剛剛意識到我應該一直在web.config文件中使用System.Web.Security.WindowsTokenRoleProvider和System.Web.Security.ActiveDirectoryMembershipProvider(而不是上面的代碼)! –

相關問題