Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change e-mail or password in AspNetUsers tables in ASP.NET Identity 2.0

Tags:

asp.net-mvc

I have implemented the ForgotPassword (with token reset) into my MVC 5 application. We are in production. Although this works in majority of the cases, many of our end-users are of older age and get confused when they cannot login and need a reset. So in those situations, I am considering giving one of our admin staff the ability to reset a user's password and giving them the new password on the phone. The data is not that sensitive.

I tried this:

    public ActionResult ResetPassword()
    {     UserManager<IdentityUser> userManager =
            new UserManager<IdentityUser>(new UserStore<IdentityUser>());

        var user = userManager.FindByEmail("useremail.samplecom");

        userManager.RemovePassword(user.Id);
        userManager.AddPassword(user.Id, "newpassword");
}

I get a cryptic error stating Invalid Column EMail, Invalid Column Email Confirmed ......

I also tried the userManager.ResetPassword(), but abandoned that idea because it needs a token reset. I want to bypass it.

What am I not seeing?

Thanks in advance.

like image 595
SKale Avatar asked Feb 12 '15 23:02

SKale


1 Answers

I also tried the userManager.ResetPassword(), but abandoned that idea because it needs a token reset. I want to bypass it.

How about you just generate the token and pass it to the Reset routine ?

var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var code = await userManager.GeneratePasswordResetTokenAsync("username");
var result = await userManager.ResetPasswordAsync("username", code, "your new password");
if (!result.Succeeded)
{
    //password does not meet standards
}

The idea here is you are just emulating/bypassing the usual routine of sending the token to the client (via email) and having the link that they click on call ResetPasswordAsync

like image 118
wal Avatar answered Sep 18 '22 10:09

wal