Esempio n. 1
0
        public void UpdateUser(User userObject)
        {
            UserPrincipal usr = null;

            try
            {
                if (string.IsNullOrEmpty(userObject.UserPrincipalName))
                    throw new MissingFieldException("User", "UserPrincipalName");

                if (string.IsNullOrEmpty(userObject.Firstname))
                    throw new MissingFieldException("User", "FirstName");

                if (string.IsNullOrEmpty(userObject.DisplayName))
                    throw new MissingFieldException("User", "DisplayName");

                log.DebugFormat("Updating user {0} values...", userObject.UserPrincipalName);

                pc = GetPrincipalContext();
                usr = UserPrincipal.FindByIdentity(pc, IdentityType.UserPrincipalName, userObject.UserPrincipalName);
                if (usr == null)
                    throw new NoMatchingPrincipalException(userObject.UserPrincipalName);

                DirectoryEntry deEntry = usr.GetUnderlyingObject() as DirectoryEntry;
                deEntry.Properties["givenName"].Value = userObject.Firstname;
                deEntry.Properties["DisplayName"].Value = userObject.DisplayName;

                SetPropertyValue(ref deEntry, "sn", userObject.Lastname);
                SetPropertyValue(ref deEntry, "streetAddress", userObject.Street);
                SetPropertyValue(ref deEntry, "l", userObject.City);
                SetPropertyValue(ref deEntry, "st", userObject.State);
                SetPropertyValue(ref deEntry, "postalCode", userObject.PostalCode);
                SetPropertyValue(ref deEntry, "postOfficeBox", userObject.POBox);
                SetPropertyValue(ref deEntry, "co", userObject.Country);
                SetPropertyValue(ref deEntry, "c", userObject.CountryCode);
                SetPropertyValue(ref deEntry, "department", userObject.Department);
                SetPropertyValue(ref deEntry, "company", userObject.Company);
                SetPropertyValue(ref deEntry, "description", userObject.Description);
                SetPropertyValue(ref deEntry, "title", userObject.JobTitle);
                SetPropertyValue(ref deEntry, "facsimileTelephoneNumber", userObject.Fax);
                SetPropertyValue(ref deEntry, "homePhone", userObject.HomePhone);
                SetPropertyValue(ref deEntry, "mobile", userObject.MobilePhone);
                SetPropertyValue(ref deEntry, "pager", userObject.Pager);
                SetPropertyValue(ref deEntry, "ipPhone", userObject.IPPhone);
                SetPropertyValue(ref deEntry, "physicalDeliveryOfficeName", userObject.Office);
                SetPropertyValue(ref deEntry, "info", userObject.Notes);
                SetPropertyValue(ref deEntry, "wWWHomePage", userObject.Webpage);

                deEntry.CommitChanges();
                log.InfoFormat("Successfully updated user {0}", userObject.UserPrincipalName);
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error updating {0}. Exception: {1}", userObject.UserPrincipalName, ex.ToString());
                throw;
            }
            finally
            {
                if (usr != null)
                    usr.Dispose();
            }
        }
Esempio n. 2
0
        public User GetUserWithPhoto(string username)
        {
            UserPrincipal usr = null;

            User foundUser = new User();
            try
            {
                pc = GetPrincipalContext();

                log.DebugFormat("Attempting to retrieve user {0}", username);
                usr = UserPrincipal.FindByIdentity(pc, IdentityType.UserPrincipalName, username);

                DirectoryEntry tmp = (DirectoryEntry)usr.GetUnderlyingObject();
                foundUser.AccountExpires = GetPropertyValue(ref tmp, "accountExpires", "long");
                foundUser.BadPasswordTime = GetPropertyValue(ref tmp, "badPasswordTime", "long");
                foundUser.BadPwdCount = GetPropertyValue(ref tmp, "badPwdCount", "int");
                foundUser.UserAccountControl = GetPropertyValue(ref tmp, "userAccountControl", "int");
                foundUser.PwdLastSet = GetPropertyValue(ref tmp, "pwdLastSet", "long");
                foundUser.SamAccountType = GetPropertyValue(ref tmp, "sAMAccountType", "int");
                foundUser.UserGuid = GetPropertyValue(ref tmp, "objectGuid");
                foundUser.Street = GetPropertyValue(ref tmp, "streetAddress");
                foundUser.City = GetPropertyValue(ref tmp, "l");
                foundUser.State = GetPropertyValue(ref tmp, "st");
                foundUser.PostalCode = GetPropertyValue(ref tmp, "postalCode");
                foundUser.Country = GetPropertyValue(ref tmp, "co");
                foundUser.CountryCode = GetPropertyValue(ref tmp, "c");
                foundUser.Company = GetPropertyValue(ref tmp, "company");
                foundUser.Department = GetPropertyValue(ref tmp, "department");
                foundUser.Description = GetPropertyValue(ref tmp, "description");
                foundUser.Firstname = GetPropertyValue(ref tmp, "givenName");
                foundUser.Lastname = GetPropertyValue(ref tmp, "sn");
                foundUser.DisplayName = GetPropertyValue(ref tmp, "displayName");
                foundUser.Name = GetPropertyValue(ref tmp, "name");
                foundUser.UserPrincipalName = GetPropertyValue(ref tmp, "userPrincipalName");
                foundUser.Fax = GetPropertyValue(ref tmp, "facsimileTelephoneNumber");
                foundUser.TelephoneNumber = GetPropertyValue(ref tmp, "telephoneNumber");
                foundUser.HomePhone = GetPropertyValue(ref tmp, "homePhone");
                foundUser.IPPhone = GetPropertyValue(ref tmp, "ipPhone");
                foundUser.JobTitle = GetPropertyValue(ref tmp, "title");
                foundUser.MobilePhone = GetPropertyValue(ref tmp, "mobile");
                foundUser.Office = GetPropertyValue(ref tmp, "physicalDeliveryOfficeName");
                foundUser.Pager = GetPropertyValue(ref tmp, "pager");
                foundUser.POBox = GetPropertyValue(ref tmp, "postOfficeBox");
                foundUser.ScriptPath = GetPropertyValue(ref tmp, "scriptPath");
                foundUser.ProfilePath = GetPropertyValue(ref tmp, "profilePath");
                foundUser.Webpage = GetPropertyValue(ref tmp, "wWWHomePage");

                // Get groups
                List<string> groups = new List<string>();
                foreach (var g in usr.GetAuthorizationGroups())
                {
                    groups.Add(g.Name);
                }
                foundUser.MemberOf = groups.ToArray();

                // Get photo
                if (tmp.Properties["thumbnailPhoto"] != null && tmp.Properties["thumbnailPhoto"].Count > 0)
                {
                    foundUser.ImageFromAD = tmp.Properties["thumbnailPhoto"][0] as byte[];
                }

                return foundUser;
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error retrieving user {0}. Exception: {1}", username, ex.ToString());
                throw;
            }
            finally
            {
                if (usr != null)
                    usr.Dispose();
            }
        }
Esempio n. 3
0
        public User Create(string usersOU, string clearTextPassword, User userObject)
        {
            PrincipalContext ctx = null;
            UserPrincipalExt usr = null;

            try
            {
                log.Debug("Attempting to create new user");

                if (string.IsNullOrEmpty(usersOU))
                    throw new MissingFieldException("User", "usersOU");

                if (string.IsNullOrEmpty(clearTextPassword))
                    throw new MissingFieldException("User", "clearTextPassword");

                if (string.IsNullOrEmpty(userObject.sAMAccountName))
                    throw new MissingFieldException("User", "SamAccountName");

                if (string.IsNullOrEmpty(userObject.UserPrincipalName))
                    throw new MissingFieldException("User", "UserPrincipalName");

                if (string.IsNullOrEmpty(userObject.Firstname))
                    throw new MissingFieldException("User", "FirstName");

                if (string.IsNullOrEmpty(userObject.DisplayName))
                    throw new MissingFieldException("User", "DisplayName");

                if (string.IsNullOrEmpty(userObject.Name))
                    throw new MissingFieldException("User", "Name");

                // Check if the user exists
                pc = GetPrincipalContext(); // Used for querying purposes
                usr = UserPrincipalExt.FindByIdentity(pc, IdentityType.UserPrincipalName, userObject.UserPrincipalName);
                if (usr != null)
                    throw new PrincipalExistsException(userObject.UserPrincipalName);

                // Now we can create the user!
                userObject.sAMAccountName = GetAvailableSamAccountName(userObject.UserPrincipalName);
                ctx = new PrincipalContext(ContextType.Domain, this._domainController, usersOU, this._username, this._password); // Used for creating new user
                usr = new UserPrincipalExt(ctx, userObject.sAMAccountName, clearTextPassword, true);
                usr.UserPrincipalName = userObject.UserPrincipalName;
                usr.DisplayName = userObject.DisplayName;
                usr.Name = userObject.Name;
                usr.GivenName = userObject.Firstname;

                if (!string.IsNullOrEmpty(userObject.Lastname))
                    usr.LastName = userObject.Lastname;

                if (!string.IsNullOrEmpty(userObject.Department))
                    usr.Department = userObject.Department;

                usr.Save();

                // After we save we need to return some data
                userObject.UserGuid = (Guid)usr.Guid;
                userObject.DistinguishedName = usr.DistinguishedName;

                return userObject;
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error retrieving user {0}. Exception: {1}", userObject.UserPrincipalName, ex.ToString());
                throw;
            }
            finally
            {
                if (usr != null)
                    usr.Dispose();
            }
        }
Esempio n. 4
0
        public User GetUserWithoutGroups(string username)
        {
            UserPrincipal usr = null;

            User foundUser = new User();
            try
            {
                pc = GetPrincipalContext();

                log.DebugFormat("Attempting to retrieve user {0}", username);
                usr = UserPrincipal.FindByIdentity(pc, IdentityType.UserPrincipalName, username);

                DirectoryEntry tmp = (DirectoryEntry)usr.GetUnderlyingObject();
                //foundUser.AccountExpires = GetPropertyValue(ref tmp, "accountExpires", "long");
                //foundUser.BadPasswordTime = GetPropertyValue(ref tmp, "badPasswordTime", "long");
                foundUser.BadPwdCount = GetPropertyValue(ref tmp, "badPwdCount", "int");
                foundUser.UserAccountControl = GetPropertyValue(ref tmp, "userAccountControl", "int");
                //foundUser.PwdLastSet = GetPropertyValue(ref tmp, "pwdLastSet", "long");
                foundUser.SamAccountType = GetPropertyValue(ref tmp, "sAMAccountType", "int");
                foundUser.UserGuid = tmp.Guid;
                foundUser.Street = GetPropertyValue(ref tmp, "streetAddress");
                foundUser.City = GetPropertyValue(ref tmp, "l");
                foundUser.State = GetPropertyValue(ref tmp, "st");
                foundUser.PostalCode = GetPropertyValue(ref tmp, "postalCode");
                foundUser.Country = GetPropertyValue(ref tmp, "co");
                foundUser.CountryCode = GetPropertyValue(ref tmp, "c");
                foundUser.Company = GetPropertyValue(ref tmp, "company");
                foundUser.Department = GetPropertyValue(ref tmp, "department");
                foundUser.Description = GetPropertyValue(ref tmp, "description");
                foundUser.Firstname = GetPropertyValue(ref tmp, "givenName");
                foundUser.Lastname = GetPropertyValue(ref tmp, "sn");
                foundUser.DisplayName = GetPropertyValue(ref tmp, "displayName");
                foundUser.Name = GetPropertyValue(ref tmp, "name");
                foundUser.UserPrincipalName = GetPropertyValue(ref tmp, "userPrincipalName");
                foundUser.Fax = GetPropertyValue(ref tmp, "facsimileTelephoneNumber");
                foundUser.TelephoneNumber = GetPropertyValue(ref tmp, "telephoneNumber");
                foundUser.HomePhone = GetPropertyValue(ref tmp, "homePhone");
                foundUser.IPPhone = GetPropertyValue(ref tmp, "ipPhone");
                foundUser.JobTitle = GetPropertyValue(ref tmp, "title");
                foundUser.MobilePhone = GetPropertyValue(ref tmp, "mobile");
                foundUser.Office = GetPropertyValue(ref tmp, "physicalDeliveryOfficeName");
                foundUser.Pager = GetPropertyValue(ref tmp, "pager");
                foundUser.POBox = GetPropertyValue(ref tmp, "postOfficeBox");
                foundUser.ScriptPath = GetPropertyValue(ref tmp, "scriptPath");
                foundUser.ProfilePath = GetPropertyValue(ref tmp, "profilePath");
                foundUser.Webpage = GetPropertyValue(ref tmp, "wWWHomePage");

                int flags = (int)tmp.Properties["userAccountControl"].Value;
                foundUser.IsEnabled = !Convert.ToBoolean(flags & 0x0002);

                return foundUser;
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error retrieving user {0}. Exception: {1}", username, ex.ToString());
                throw;
            }
            finally
            {
                if (usr != null)
                    usr.Dispose();
            }
        }
        public List<User> GetUsers(string distinguishedName)
        {
            DirectorySearcher dr = null;

            List<User> foundUsers = new List<User>();
            try
            {
                if (string.IsNullOrEmpty(distinguishedName))
                    throw new MissingFieldException("OrganizationalUnits", "DistinguishedName");

                log.DebugFormat("Retrieving a list of users from {0}", distinguishedName);

                de = GetDirectoryEntry(distinguishedName);
                dr = new DirectorySearcher(de, "(objectClass=user)", null, SearchScope.Subtree);

                foreach (var user in dr.FindAll())
                {
                    // Get our organizational unit
                    DirectoryEntry tmp = (DirectoryEntry)user;

                    User foundUser = new User();
                    foundUser.AccountExpires = GetPropertyValue(ref tmp, "accountExpires", "long");
                    foundUser.BadPasswordTime = GetPropertyValue(ref tmp, "badPasswordTime", "long");
                    foundUser.BadPwdCount = GetPropertyValue(ref tmp, "badPwdCount", "int");
                    foundUser.UserAccountControl = GetPropertyValue(ref tmp, "userAccountControl", "int");
                    foundUser.PwdLastSet = GetPropertyValue(ref tmp, "pwdLastSet", "long");
                    foundUser.SamAccountType = GetPropertyValue(ref tmp, "sAMAccountType", "int");
                    foundUser.UserGuid = GetPropertyValue(ref tmp, "objectGuid");
                    foundUser.Street = GetPropertyValue(ref tmp, "streetAddress");
                    foundUser.City = GetPropertyValue(ref tmp, "l");
                    foundUser.State = GetPropertyValue(ref tmp, "st");
                    foundUser.PostalCode = GetPropertyValue(ref tmp, "postalCode");
                    foundUser.Country = GetPropertyValue(ref tmp, "co");
                    foundUser.CountryCode = GetPropertyValue(ref tmp, "c");
                    foundUser.Company = GetPropertyValue(ref tmp, "company");
                    foundUser.Department = GetPropertyValue(ref tmp, "department");
                    foundUser.Description = GetPropertyValue(ref tmp, "description");
                    foundUser.Firstname = GetPropertyValue(ref tmp, "givenName");
                    foundUser.Lastname = GetPropertyValue(ref tmp, "sn");
                    foundUser.DisplayName = GetPropertyValue(ref tmp, "displayName");
                    foundUser.Name = GetPropertyValue(ref tmp, "name");
                    foundUser.UserPrincipalName = GetPropertyValue(ref tmp, "userPrincipalName");
                    foundUser.Fax = GetPropertyValue(ref tmp, "facsimileTelephoneNumber");
                    foundUser.TelephoneNumber = GetPropertyValue(ref tmp, "telephoneNumber");
                    foundUser.HomePhone = GetPropertyValue(ref tmp, "homePhone");
                    foundUser.IPPhone = GetPropertyValue(ref tmp, "ipPhone");
                    foundUser.JobTitle = GetPropertyValue(ref tmp, "title");
                    foundUser.MobilePhone = GetPropertyValue(ref tmp, "mobile");
                    foundUser.Office = GetPropertyValue(ref tmp, "physicalDeliveryOfficeName");
                    foundUser.Pager = GetPropertyValue(ref tmp, "pager");
                    foundUser.POBox = GetPropertyValue(ref tmp, "postOfficeBox");
                    foundUser.ScriptPath = GetPropertyValue(ref tmp, "scriptPath");
                    foundUser.ProfilePath = GetPropertyValue(ref tmp, "profilePath");
                    foundUser.Webpage = GetPropertyValue(ref tmp, "wWWHomePage");

                    foundUsers.Add(foundUser);
                }

                return foundUsers;
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error retrieving users from {0}. Exception: {1}", distinguishedName, ex.ToString());
                throw;
            }
            finally
            {
                if (dr != null)
                    dr.Dispose();
            }
        }