コード例 #1
0
        /// <summary>
        /// Gets detailed user information about a specific user
        /// </summary>
        /// <param name="userPrincipalName"></param>
        /// <returns></returns>
        public UsersObject GetUser(string userPrincipalName)
        {
            PrincipalContext pc = null;
            UserPrincipalExt up = null;

            try
            {
                pc = new PrincipalContext(ContextType.Domain, this.domainController, this.username, this.password);

                logger.Debug("Attempting to retrieve user " + userPrincipalName);

                up = UserPrincipalExt.FindByIdentity(pc, IdentityType.UserPrincipalName, userPrincipalName);
                if (up == null)
                {
                    throw new Exception("USER_NOT_FOUND");
                }
                else
                {
                    UsersObject returnUser = new UsersObject();
                    returnUser.UserPrincipalName = up.UserPrincipalName;
                    returnUser.sAMAccountName    = up.SamAccountName;
                    returnUser.Firstname         = up.GivenName;
                    returnUser.Middlename        = up.MiddleName;
                    returnUser.Lastname          = up.Surname;
                    returnUser.DisplayName       = up.DisplayName;
                    returnUser.Department        = up.Department;
                    returnUser.IsEnabled         = up.Enabled == null ? true : (bool)up.Enabled;

                    return(returnUser);
                }
            }
            catch (Exception ex)
            {
                this.logger.Error("Error retrieving user information " + userPrincipalName, ex);

                throw;
            }
            finally
            {
                if (up != null)
                {
                    up.Dispose();
                }

                if (pc != null)
                {
                    pc.Dispose();
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Updates a user in Active Directory
        /// </summary>
        /// <param name="user"></param>
        /// <param name="isUsingDisplayNameAsNameAttribute"></param>
        public void UpdateUser(UsersObject user, bool isUsingDisplayNameAsNameAttribute)
        {
            PrincipalContext pc = null;
            UserPrincipalExt up = null;

            try
            {
                pc = new PrincipalContext(ContextType.Domain, this.domainController, this.username, this.password);

                logger.Debug("Finding user in Active Directory: " + user.UserPrincipalName);

                up = UserPrincipalExt.FindByIdentity(pc, IdentityType.UserPrincipalName, user.UserPrincipalName);
                if (up == null)
                {
                    throw new Exception("USER IS UNKNOWN");
                }
                else
                {
                    up.GivenName   = user.Firstname;
                    up.DisplayName = user.DisplayName;
                    up.Enabled     = user.IsEnabled;

                    if (!string.IsNullOrEmpty(user.Middlename))
                    {
                        up.MiddleName = user.Middlename;
                    }
                    else
                    {
                        up.MiddleName = null;
                    }

                    if (!string.IsNullOrEmpty(user.Lastname))
                    {
                        up.LastName = user.Lastname;
                    }
                    else
                    {
                        up.LastName = null;
                    }

                    if (!string.IsNullOrEmpty(user.Department))
                    {
                        up.Department = user.Department;
                    }
                    else
                    {
                        up.Department = null;
                    }

                    if (isUsingDisplayNameAsNameAttribute)
                    {
                        up.Name = user.DisplayName;
                    }

                    // Save changes
                    up.Save();
                }
            }
            catch (Exception ex)
            {
                this.logger.Error("Error updating user " + user.UserPrincipalName, ex);

                throw;
            }
            finally
            {
                if (up != null)
                {
                    up.Dispose();
                }

                if (pc != null)
                {
                    pc.Dispose();
                }
            }
        }