-2

How can I change the user password to null from admin page using user id`?

I have this code in the login model:

public string userId { get; set; }
public string userPassword { get; set; }
public string userName { get; set; }

And in the reset model:

public string userId{ get; set; }
[Compare("userId", ErrorMessage = "not matched")]
public string confirmId{ get; set; }

In the controller:

public ActionResult reset([Bind(Include = "userId,confirmId")] Login model1, reset model2) { 
    bool userExist = dbcontext.Login.Any(contact => contact.userId.Equals(model2.userId));
    if (contactExists && model2.userId==model2.confirmId) {
        dbcontext.Entry(model1).Property(x => x.LoginPassword).IsModified = true;
        dbcontext.Entry(model1).Property(x => x.Item1.EmployeeName).IsModified = false;
        dbcontext.Entry(model1).State = EntityState.Modified;
        dbcontext.SaveChanges();
    }
}

In the view I have this code:

@model BSR.Models.reset
@using (Html.BeginForm()) {
    textbox for userId
    textbox for confirmId
    submitt button
}

I tried to assign null to userPassword but it didn't accept null in the database table. I allowed it to be null, what should I write to change the userPassowrd to null and leave the rest fields in the row as they are, unchanged?

Antti29
  • 2,885
  • 12
  • 35
  • 36
Developer
  • 13
  • 8
  • A null password is absurd. How would you enter `NULL` in login form? – Dan Wilson Sep 24 '17 at 18:35
  • if the user forgot his password and contacted the admin then admin can reset it as userId but the userPassword should be null, the userid and userpassword can be same as userid , i made the password null so later can the user change it and i keep the hash password in the table – Developer Sep 24 '17 at 20:23

1 Answers1

0

try using the user store: (ASP.NET Identity reset password )

UserManager<IdentityUser> userManager = 
new UserManager<IdentityUser>(new UserStore<IdentityUser>());

userManager.RemovePassword(userId);

userManager.AddPassword(userId, newPassword);
Waqas Amjad
  • 195
  • 1
  • 13