Importing users

DVS Developers asked on June 9, 2015 18:27

Hello,

I have imported my old users into CMSUsers. I have added the following loader attribute:

public override void Init()
    {
        SecurityEvents.Authenticate.Execute += OnAuthenticateExecute;
    }

    private void OnAuthenticateExecute(object sender, AuthenticationEventArgs e)
    {
        try
        {
            UserInfo objUserInfo = UserInfoProvider.GetUserInfo(e.UserName);
            if (objUserInfo == null)
                return; // user doesn't exist

            bool blnIsImportedUser = objUserInfo.GetValue<bool>("IsImportedUser", false); // is this an imported user?
            if (blnIsImportedUser)
            {
                string strEncryptedPassword = objUserInfo.GetValue<string>("ImportedEncryptedPassword", string.Empty); // get the imported password
                string strPasswordSalt = objUserInfo.GetValue<string>("ImportedPasswordSalt", string.Empty); // get the imported salt

                if (string.IsNullOrWhiteSpace(strEncryptedPassword) || string.IsNullOrWhiteSpace(strPasswordSalt))
                    return; // pass and/or salt not set

                ImportedAuthentication objImportedAuth = new ImportedAuthentication(); // use old authentication
                if (strEncryptedPassword.Equals(objImportedAuth.EncodePassword(e.Password, strPasswordSalt))) // is password correct?
                {
                    UserInfoProvider.SetPassword(objUserInfo, e.Password); // set the kentico password to the same as the user enty
                    objUserInfo.SetValue("IsImportedUser", false); // remove old user flag
                    objUserInfo.Update(); // save to kentico
                }
            }
        }
        catch (Exception ex)
        {
            ErrorHandler.HandleError(ex);
        }
    }

When an imported user attempts to log in, the code acts as expected. The kentico password is set and the flag is unset. However the authentication fails. On the next attempt, the authentication is successful. My guess is the authentication has already failed before it gets to my code. Is there a way of changing the password before kentico authenticates the user?

Thanks

Recent Answers


Brenden Kehren answered on June 9, 2015 19:49

After your .Update() call, authenticate the user again. Your code should not run again since your flag is set to false.

0 votesVote for this answer Mark as a Correct answer

DVS Developers answered on June 10, 2015 09:35

How do you do the authentication again? The user is going via the normal kentico login page.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on June 10, 2015 13:38

AuthenticationHelper.AuthenticateUser(userName, true);

0 votesVote for this answer Mark as a Correct answer

DVS Developers answered on June 11, 2015 12:44

Still have the same problem. Click login the first time it says login failed. Click again using the same password it logs in.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.