2017-09-24 88 views
-2

如何使用用户标识从管理页面更改用户密码为null如何允许管理员在MVC中将用户密码重置为空

我有这样的代码在登录模式:

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 
} 

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

+0

空口令是荒谬的。你将如何在登录表单中输入NULL? –

+0

如果用户忘记了他的密码并联系了管理员,那么管理员可以将其重置为userId,但userPassword应该为空,用户标识和用户密码可以与用户标识相同,我将密码设置为空,以便用户随后可以更改它并保留表中的散列密码 – Nadz

回答

0

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

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

userManager.RemovePassword(userId); 

userManager.AddPassword(userId, newPassword); 
相关问题