2017-09-24 89 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); 
相關問題