【问题标题】:How to allow the admin to reset a user password to null in MVC如何允许管理员在 MVC 中将用户密码重置为空
【发布时间】:2018-03-05 13:52:17
【问题描述】:

如何使用用户 ID 将用户密码从管理页面更改为 null?

我在登录模型中有这段代码:

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

在重置模型中:

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

在控制器中:

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();
    }
}

在视图中我有这个代码:

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

我尝试将null 分配给userPassword,但它不接受数据库表中的null。我允许它是null,我应该写什么来将userPassowrd 更改为null 并将行中的其余字段保持不变?

【问题讨论】:

  • 空密码是荒谬的。您将如何在登录表单中输入NULL?
  • 如果用户忘记了密码并联系了管理员,那么管理员可以将其重置为 userId 但 userPassword 应该为 null,userid 和 userpassword 可以与 userid 相同,我将密码设置为 null 以便稍后可以用户更改它,我将哈希密码保存在表中

标签: c# asp.net-mvc


【解决方案1】:

尝试使用用户存储:(ASP.NET Identity reset password)

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

userManager.RemovePassword(userId);

userManager.AddPassword(userId, newPassword);

【讨论】:

    猜你喜欢
    • 2018-10-19
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2012-11-19
    • 2011-09-15
    相关资源
    最近更新 更多