Exemple #1
0
        public string UpdateLdapAccount(UserInfo oldUserInfo, UserInfo newUserInfo)
        {
            var user = LdapHelper.GetLdapUser(oldUserInfo, RootPrincipal);

            try
            {
                if (user != null)
                {
                    var preNewUserInfo = newUserInfo;
                    preNewUserInfo.SamName = user.SamAccountName;
                    if (newUserInfo.FirstName.ToLower() != user.GivenName.ToLower() || newUserInfo.LastName.ToLower() != user.Surname.ToLower())
                    {
                        preNewUserInfo = LdapHelper.GetUniqueFirstNameLastName(newUserInfo, RootPrincipal);
                    }

                    using (DirectoryEntry entry = (DirectoryEntry)user.GetUnderlyingObject())
                    {
                        entry.InvokeSet("sAMAccountName", preNewUserInfo.SamName);
                        entry.InvokeSet("sn", preNewUserInfo.LastName);
                        entry.InvokeSet("givenName", preNewUserInfo.FirstName);
                        entry.InvokeSet("userPrincipalName", preNewUserInfo.Email);
                        if (!String.IsNullOrEmpty(newUserInfo.Password))
                        {
                            entry.Invoke("SetPassword", new object[] { newUserInfo.Password });
                        }
                        entry.InvokeSet("displayName", preNewUserInfo.SamName);
                        entry.InvokeSet("mail", preNewUserInfo.Email);
                        entry.CommitChanges();
                        entry.Rename("CN=" + preNewUserInfo.SamName);
                        entry.CommitChanges();
                    }

                    //user.SamAccountName = preNewUserInfo.SamName;
                    //user.DisplayName = String.Format("{0} {1}", preNewUserInfo.FirstName, newUserInfo.LastName);
                    //user.Surname = preNewUserInfo.LastName;
                    //user.GivenName = preNewUserInfo.FirstName;
                    //user.UserPrincipalName = preNewUserInfo.Email;
                    //user.EmailAddress = preNewUserInfo.Email;
                    //if (!String.IsNullOrEmpty(newUserInfo.Password))
                    //{
                    //    user.Enabled = true;
                    //    user.PasswordNeverExpires = true;
                    //    user.SetPassword(newUserInfo.Password);
                    //}
                    //user.Save();

                    return(string.Empty);
                }
                return(string.Empty);
            }
            catch (Exception ex)
            {
                var error = String.Format("Exception updating email address for {0} to {1} - {2}", oldUserInfo.Email, newUserInfo.Email, ex.Message);
                return(error);
            }
        }
Exemple #2
0
        public AccountStatus CreateNewLdapAccount(UserInfo userInfo, out string errorText, bool pswdPolicyChk = false)
        {
            errorText = string.Empty;
            if (LdapHelper.LdapAccountExists(userInfo, RootPrincipal))
            {
                return(AccountStatus.AccountAlreadyExists);
            }

            try
            {
                userInfo.FirstName = LdapHelper.EscapeChars(userInfo.FirstName);
                userInfo.LastName  = LdapHelper.EscapeChars(userInfo.LastName);
                var preNewUserInfo = LdapHelper.GetUniqueFirstNameLastName(userInfo, RootPrincipal);
                var newUser        = new UserPrincipal(RootPrincipal)
                {
                    SamAccountName    = preNewUserInfo.SamName,
                    DisplayName       = String.Format("{0} {1}", preNewUserInfo.FirstName, preNewUserInfo.LastName),
                    Surname           = preNewUserInfo.LastName,
                    GivenName         = preNewUserInfo.FirstName,
                    UserPrincipalName = preNewUserInfo.Email,
                    EmailAddress      = preNewUserInfo.Email,
                };

                if (!String.IsNullOrEmpty(userInfo.Password))
                {
                    newUser.Enabled = true;
                    newUser.PasswordNeverExpires = true;
                    newUser.SetPassword(userInfo.Password);
                }
                else
                {
                    newUser.ExpirePasswordNow();
                }
                newUser.Save();
                return(AccountStatus.NewAccount);
            }
            catch (Exception ex)
            {
                errorText = String.Format("Exception creating LDAP account for {0} with exception {1}", userInfo.Email, ex.Message);
                return(AccountStatus.AccountCreationFailed);
            }
        }