Search code examples
c#asp.netasp.net-identity-2

"The input is not a valid Base-64 string" exception on login after migrating from membership to identity 2.0


I just upgraded my website from memberhip to asp.net identity 2.0 based on the instructions found here. I created new register and login forms, and migrated my user data to the new db tables.

If I create a new user using the register.aspx form, everything works fine. The new user is saved to the db, and I can log in with the credentials.

But if I try to log in with an existing user that I migrated, I get the following System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

From Login.aspx.cs

protected void SignIn(object sender, EventArgs e)
    {
        var userStore = new UserStore<User>(new ApplicationDBContext());
        var userManager = new UserManager<User>(userStore);
        var user = userManager.Find(UserName.Text, Password.Text);

        if (user != null)
        {
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

            authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity);
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            StatusText.Text = "Invalid username or password.";
            LoginStatus.Visible = true;
        }
    }

The exception occurs on this line:

var user = userManager.Find(UserName.Text, Password.Text);

I set a breakpoint to see what it was bombing on, but it just got to that line in the code and stopped.

I thought it might be due to the way I migrated the password, since new users work, but I don't see anything that matches the error in the PasswordHash column for existing users.

This is what I inserted into the PasswordHash column when I migrated the users:

(aspnet_Membership.Password+'|'+CAST(aspnet_Membership.PasswordFormat as varchar)+'|'+aspnet_Membership.PasswordSalt)

I've been working this all week, any help would be greatly appreciated.

Kris


Solution

  • In the Login.aspx.cs file, I changed

    var userManager = new UserManager<User>(userStore);
    

    to

    var userManager = new UserManager();
    

    This allowed me to login with both existing and new users by using my UserManager instead of identity's.

    Thanks to trailmax for getting me in the right direction!