簡體   English   中英

如何創建自定義Active Directory權限和組,以便以此驗證用戶?

[英]How do I create a custom Active Directory permission and group so that I can verify users against this?

我看過許多搜索結果,但我一直在努力尋找一種方法,以編程方式(使用C#)在Active Directory中創建自定義權限和自定義組。

我有一個應用程序,它將需要大約50個單獨的權限,例如:可以加密數據,可以解密數據,可以導出私鑰,可以刪除密鑰對等。這些權限將分配給自定義組。 例如,該組可以稱為:標准用戶,安全管理員等。

將為用戶分配一個或多個這些組。 我需要通過Active Directory管理所有這些。 正在編寫的軟件在C#中。 用戶將位於Active Directory中。

當要在應用程序上執行功能時,軟件將檢查用戶是否具有特定權限。 如果用戶沒有權限,則將要求他們輸入替代。 此覆蓋只是提示,提示確實擁有相關權限的另一個用戶的憑據。

我想強調指出,這需要通過Active Directory進行管理,因為該軟件在域上運行,並且權限將由域管理員管理。

這樣,我相信ASP.Net角色功能還不夠? 此外,我不確定Azure AD是否與Windows AD相同。

我非常感謝有關.NET程序集/命名空間將提供以下功能的任何指導:

  • 建立權限
  • 創建組
  • 分配權限到組
  • 將用戶分配給組
  • 從群組中刪除用戶
  • 從群組中刪除權限

我需要以編程方式進行此操作,因為該軟件將具有安裝程序,並將負責在安裝過程中添加特定於應用程序的自定義權限和組(如果尚不存在)。

我可能會遇到這種錯誤,因此我願意接受其他建議。 只要我能夠執行以上操作,那就太好了!

謝謝!

據我了解,

在這里您可以嘗試下面的代碼

嘗試一次

1)建立群組

                PrincipalContext principalContext =
                    new PrincipalContext(ContextType.Domain, LDAPDomain, LDAPContainer,
                        LDAPAdmin, LDAPPassword);

                GroupPrincipal group = GroupPrincipal.FindByIdentity(principalContext, "groupName");

                if (group == null)
                {
                    GroupPrincipal groupPrincipal = new GroupPrincipal(principalContext);
                    groupPrincipal.Name = "groupName";
                    groupPrincipal.SamAccountName = "samAccountName";
                    groupPrincipal.UserPrincipalName = "userPrincipleName";
                    groupPrincipal.GroupScope = GroupScope.Global;
                    groupPrincipal.Description = "groupNameDescription";
                    groupPrincipal.DisplayName = "groupNameDisplayName";
                    groupPrincipal.Save();
                }

2)將用戶添加到組

            GroupPrincipal group = GroupPrincipal.FindByIdentity(principalContext, "groupName");
            UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, "userName");

            bool isUserAdded = false;


            if (user != null & group != null)
            {
                if (user.IsMemberOf(group))
                {
                    //Do Code
                }
                else
                {
                    group.Members.Add(user);
                    group.Save();
                    isUserAdded = user.IsMemberOf(group);
                }
            }

            if (isUserAdded)
            {
                //Do Code
            }

3)從組中刪除用戶

                GroupPrincipal group = GroupPrincipal.FindByIdentity(principalContext, "groupName");
                UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, "userName");

                bool isUserRemoved = false;


                if (user != null & group != null)
                {
                    if (user.IsMemberOf(group))
                    {
                        group.Members.Remove(user);
                        group.Save();
                        isUserRemoved = user.IsMemberOf(group);
                    }
                    else
                    {
                        //Do Code

                    }
                }

                if (!isUserRemoved)
                {
                    //Do Code
                }

4)添加或刪除AccessRule(Permission)到組

就我而言,我不清楚您的邏輯或實現到底是什么,

但是在這里,我嘗試提供一種為組添加或刪除訪問規則的解決方案

            //DirectoryEntry for OU Level
            DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://OU=MYOU,DC=MYDC,DC=COM");

            NTAccount account = new NTAccount("MYDC", "groupName");

            ActiveDirectoryAccessRule ruleRead = new ActiveDirectoryAccessRule(
                account,
                ActiveDirectoryRights.ReadProperty,
                AccessControlType.Allow,
                ActiveDirectorySecurityInheritance.None);

            ActiveDirectoryAccessRule ruleWrite = new ActiveDirectoryAccessRule(
                account,
                ActiveDirectoryRights.WriteProperty,
                AccessControlType.Deny,
                ActiveDirectorySecurityInheritance.None);

            if (Permission == "User shall be able to export private key from an RSA keypair")
            {
                directoryEntry.ObjectSecurity.AddAccessRule(ruleRead);

                directoryEntry.ObjectSecurity.AddAccessRule(ruleWrite);

                directoryEntry.Options.SecurityMasks = SecurityMasks.Dacl;

                directoryEntry.CommitChanges();

                Console.WriteLine("Added Deny Access to Read & Write.");
            }

            if (Permission == "User is able to decrypt imported data")
            {
                directoryEntry.ObjectSecurity.RemoveAccessRule(ruleRead);

                directoryEntry.ObjectSecurity.RemoveAccessRule(ruleWrite);

                directoryEntry.Options.SecurityMasks = SecurityMasks.Dacl;

                directoryEntry.CommitChanges();

                Console.WriteLine("Removed Deny Access to Read & Write.");
            }

            directoryEntry.Close();

            directoryEntry.Dispose();

注意:請首先在測試環境中測試上述所有代碼。

如果我正確遵循此問題,則您正在嘗試將AD用作AuthZ存儲和AuthZ引擎。 前者(將其用作數據存儲)非常有意義,但是,我認為這不是評估應用訪問權限的正確工具。

我要做的是將您的小組分為兩個層次:

  • 級別1-權限組(例如,可以加密,可以解密等)

  • 級別2-角色-這些是各種權限組的成員,並且依次將用戶添加到這些組中以向他們授予角色。 他們將繼承Windows生成其登錄令牌時角色所具有的權限。

假設您的應用程序使用Windows身份驗證,則WindowsTokenRoleProviderhttps://msdn.microsoft.com/en-us/library/system.web.security.windowstokenroleprovider ( v= WindowsTokenRoleProvider ) WindowsTokenRoleProvider )將顯示所有組成員身份進入您的應用,然后您可以檢查某人是否在權限組中,並允許他們做某事(或不做某事)...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM