2012-07-13 78 views
0

我在尋找解決我的問題的最佳解決方案,我們想要做的就是使用Active Directory作爲我們的基本登錄名,所以我們不必管理密碼等。但不是使用AD組,我們想創建自定義角色。所以我可能有一個角色將等於5個不同的AD組。通過Active Directory登錄使用自定義角色的最佳方式

我在做的是通過c#將特定組中的所有用戶轉儲到鏈接到roleid到userid表的自定義用戶表。任何人對此有任何想法?但是我也需要以某種方式管理自定義用戶表,如果有人從其中一個組中刪除,那麼他們需要鬆開訪問權限,不知道如何處理該用戶表。

目前我的方法看起來是這樣的:

 using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "SETON")) 
     { 
      //Gets all Users in a AD Group 
      using (GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName)) 
      { 
       //Mkae sure group is not null 
       if (grp != null) 
       { 
        foreach (Principal p in grp.GetMembers()) 
        { 
         //Sets up Variables to enter into Finance User Table 
         string UserName = p.SamAccountName; 
         string DisplayName = p.Name; 
         string emailAddress = p.UserPrincipalName; 

         //Get users detials by user 
         using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, UserName)) 
         { 
          if (userPrincipal != null) 
          {          
           //Test to see if user is in AD Group 
           bool inRole = userPrincipal.IsMemberOf(grp); 
           if (inRole) 
           { 
            //Test to See if UserName already exists in Finance User Table 
            var ds = User.GetUserList(UserName); 

            //If don't exist add them and to the new Role 
            if (ds.Tables[0].Rows.Count == 0) 
            { 
             //Add User to FinanceUSer Table 
             Seton.Roster.User user = new User(); 
             user.UserName = UserName; 
             user.Name = DisplayName;            
             int id = user.Save(); 

             //Get RoleID by RoleName with Method 
             var roleDS = SecurityRole.GetRoleList(roleName); 
             if (roleDS.Tables[0].Rows.Count > 0) 
             { 
              var roleDR = roleDS.Tables[0].Rows[0]; 
              int roleid = Convert.ToInt32(roleDR["roleid"].ToString()); 
              SecurityRoleUserLink.AddNewLink(roleid, id); 
             } 
            } 
            //if they exist just Get thier userid and add them to new Role 
            else 
            { 
             //Get UserID of existing FinanceUser 
             var userDR = ds.Tables[0].Rows[0]; 
             int id = Convert.ToInt32(userDR["userid"].ToString()); 

             //Get RoleID by RoleName with Method 
             var roleDS = SecurityRole.GetRoleList(roleName); 
             if (roleDS.Tables[0].Rows.Count > 0) 
             { 
              var roleDR = roleDS.Tables[0].Rows[0]; 
              int roleid = Convert.ToInt32(roleDR["roleid"].ToString()); 

              //Test to see if user already in this role 
              if(!SecurityRoleUserLink.UserInRole(id,roleid)) 
               SecurityRoleUserLink.AddNewLink(roleid, id); 

回答

0

我認爲你是重新發明輪子。查看產品MS Windows Authorisation Manager(AzMan),或者它的open-source equivalent

這些允許您定義「操作」(即用戶可以執行的任務),並將這些操作映射到AD組。他們會讓你做更多的事情,但至少他們肯定會滿足你的需求。

AzMan配置可以存儲在XML,數據庫或AD中,所以它在這方面也非常靈活。

0

我已經完成了幾個方面的工作,但答案實際上是關於如何實現安全性。你在做現場級別還是記錄級別?

  1. 在應用程序中緩存AD用戶帳戶和組以快速檢索。
    • 優點 - 帳戶在您的數據庫中,如果AD有問題,您不是SOL。
    • 缺點 - 需要重新填充和計劃的基礎,並能得到同步使用AD
  2. 兩個用戶和組應用程序組到域組的映射。
    • 優點 - 讓AD做你的工作,讓你不必去管理它在應用

當然還有更多的優點和缺點,但對我來說,這些脫穎而出。

我們目前使用AzMan,它不被支持 - 我知道。它在大多數情況下運行良好,但是需要考慮以及性能方面的大量來回同步,但最終還是會將其緩存到應用程序中。

您還可以在代碼plex上找到一個開源代碼:NetSqlAzMan,看起來很有希望。