コード例 #1
0
        public BaseActiveDirectoryUser GetUser(string userIdentity, bool loadSubProperties, bool loadThirdPartners)
        {
            var user          = new BaseActiveDirectoryUser();
            var userPrincipal = GetUserPrincipal(userIdentity);

            if (userPrincipal != null)
            {
                user = MapUserPrincipalToUser(userPrincipal, loadSubProperties, loadThirdPartners);
            }

            return(user);
        }
コード例 #2
0
        /// <summary>
        /// Maps a given user principal to a new user object
        /// </summary>
        private BaseActiveDirectoryUser MapUserPrincipalToUser(UserPrincipal userPrincipal, bool loadSubProperties, bool loadDirectReports)
        {
            // Creates the user
            var user = new BaseActiveDirectoryUser(
                userPrincipal.EmployeeId,
                ConvertSidToString(userPrincipal.Sid),
                userPrincipal.Context.Name,
                userPrincipal.SamAccountName,
                userPrincipal.EmailAddress,
                userPrincipal.DisplayName,
                userPrincipal.AccountExpirationDate,
                userPrincipal.GivenName,
                userPrincipal.MiddleName,
                userPrincipal.Surname,
                userPrincipal.VoiceTelephoneNumber,
                userPrincipal.Enabled);

            //Loads subproperties
            if (loadSubProperties || loadDirectReports)
            {
                //Loads user groups
                var directoryEntry = (DirectoryEntry)userPrincipal.GetUnderlyingObject();
                var groupArray     = directoryEntry.Properties[groupDirectoryKey];

                if (groupArray != null)
                {
                    var groups = (object[])groupArray.Value;

                    groups.AsParallel().ForAll(g =>
                    {
                        var adGroup = g.ToString().Substring(3).Split(',').FirstOrDefault();
                        user.ActiveDirectoryGroups.Add(new BaseActiveDirectoryGroup {
                            Id = adGroup, NameOrDescription = adGroup
                        });
                    });
                }

                //Checks if user account is locked and Loads additional data.
                user.UserExtraInfo.IsLocked = userPrincipal.IsAccountLockedOut();
                user.UserExtraInfo.Office   = (string)directoryEntry.InvokeGet(officeDirectoryKey);

                //Loads AD manager data
                user.ActiveDirectoryManager.Login = (string)directoryEntry.InvokeGet(managerKey);

                if (!string.IsNullOrEmpty(user.ActiveDirectoryManager.Login))
                {
                    var manager = GetUserPrincipal(user.ActiveDirectoryManager.Login, IdentityType.DistinguishedName);
                    if (manager != null)
                    {
                        user.ActiveDirectoryManager = new BaseActiveDirectoryUser(
                            manager.EmployeeId,
                            ConvertSidToString(manager.Sid),
                            manager.Context.Name,
                            manager.SamAccountName,
                            manager.EmailAddress,
                            manager.DisplayName,
                            manager.AccountExpirationDate,
                            manager.GivenName,
                            manager.MiddleName,
                            manager.Surname,
                            manager.VoiceTelephoneNumber,
                            manager.Enabled);
                    }
                }

                //Loads Direct Reports And Manager
                if (loadDirectReports)
                {
                    var relatedUsers = directoryEntry.Properties[directReportsKey];

                    if (relatedUsers != null && relatedUsers.Value != null)
                    {
                        // Gets the reports
                        if (relatedUsers.Value is string)
                        {
                            var pUser = GetUserPrincipal(relatedUsers.Value.ToString(), IdentityType.DistinguishedName);

                            if (pUser != null)
                            {
                                user.ActiveDirectoryThirdPartners.Add(new BaseActiveDirectoryUser(
                                                                          pUser.EmployeeId,
                                                                          ConvertSidToString(pUser.Sid),
                                                                          pUser.Context.Name,
                                                                          pUser.SamAccountName,
                                                                          pUser.EmailAddress,
                                                                          pUser.DisplayName,
                                                                          pUser.AccountExpirationDate,
                                                                          pUser.GivenName,
                                                                          pUser.MiddleName,
                                                                          pUser.Surname,
                                                                          pUser.VoiceTelephoneNumber,
                                                                          pUser.Enabled));
                            }
                        }
                        else
                        {
                            var usersArray = (object[])relatedUsers.Value;

                            foreach (var dn in usersArray)
                            {
                                var pUser = GetUserPrincipal(dn.ToString(), IdentityType.DistinguishedName);

                                if (pUser != null)
                                {
                                    user.ActiveDirectoryThirdPartners.Add(new BaseActiveDirectoryUser(
                                                                              pUser.EmployeeId,
                                                                              ConvertSidToString(pUser.Sid),
                                                                              pUser.Context.Name,
                                                                              pUser.SamAccountName,
                                                                              pUser.EmailAddress,
                                                                              pUser.DisplayName,
                                                                              pUser.AccountExpirationDate,
                                                                              pUser.GivenName,
                                                                              pUser.MiddleName,
                                                                              pUser.Surname,
                                                                              pUser.VoiceTelephoneNumber,
                                                                              pUser.Enabled));
                                }
                            }
                        }
                    }
                }
            }

            return(user);
        }