使用DirectoryEntry获取锁定状态

时间:2019-07-05 16:40:12

标签: asp.net-mvc

我正在尝试显示活动目录中的用户帐户是锁定的还是现在。我有自己的代码,但是我试图将其添加到现有视图中,但是我不确定如何做到这一点:下面是我正在尝试的内容以及两个正确的代码。

谢谢!

        Session["Locked"] = Convert.ToBoolean(directoryEntry.InvokeGet("IsAccountLocked"));




                    Original:
            public static List<User> GetallAdUsers()
            {

                List<User> AdUsers = new List<User>();
                var context = new PrincipalContext(ContextType.Domain, "domain.local");
                UserPrincipal userPrin = new UserPrincipal(context);
                userPrin.Name = "*";
                var searcher = new System.DirectoryServices.AccountManagement.PrincipalSearcher();
                searcher.QueryFilter = userPrin;
                var results = searcher.FindAll();
                foreach (Principal p in results)
                {

                    AdUsers.Add(new User
                    {
                        DisplayName = p.UserPrincipalName,
                        Samaccountname = p.SamAccountName

                    });
                }
                return AdUsers;
            }




                  I am trying to add it like this:
            public static List<User> GetallAdUsers()
            {

                List<User> AdUsers = new List<User>();
                var context = new PrincipalContext(ContextType.Domain, "domain.local");
                UserPrincipal userPrin = new UserPrincipal(context);
                userPrin.Name = "*";
                var searcher = new System.DirectoryServices.AccountManagement.PrincipalSearcher();
                searcher.QueryFilter = userPrin;
                var results = searcher.FindAll();
                foreach (Principal p in results)
                {
                    var user = UserPrincipal.FindByIdentity(context, p.UserPrincipalName);
                    DirectoryEntry directoryEntry = user.GetUnderlyingObject() as DirectoryEntry;
                    var isLocked = Convert.ToBoolean(directoryEntry.InvokeGet("IsAccountLocked"));
                    AdUsers.Add(new User
                    {
                        DisplayName = p.UserPrincipalName,
                        Samaccountname = p.SamAccountName,
                        Locked = isLocked
                    });
                }
                return AdUsers;
            }

0 个答案:

没有答案