2012-03-28 40 views
1

我正在使用密碼重置功能,並嘗試檢查以確保新密碼至少有7個字符。它會運行並將新密碼傳遞給控制器​​並將其設置爲用戶的密碼,但它只是使用輸入的內容而不是檢查來確保它符合密碼要求。感謝您的任何建議:)如何檢查以確保新密碼與C#的設定長度?

這裏的模型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.ComponentModel.DataAnnotations; 

namespace [CompanyName].Models 
{ 
    public class ResetPasswordModel 
    { 
     [Required] 
     [ValidatePasswordLength(7, ErrorMessage = "New passwords must be a minimum of 7 characters, please try a different password.")] 
     [DataType(DataType.Password)] 
     [Display(Name = "New password")] 
     public string NewPassword { get; set; } 

     [DataType(DataType.Password)] 
     [Display(Name = "Confirm new password")] 
     [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")] 
     public string ConfirmPassword { get; set; } 
    } 
} 

而這裏的頁面重置密碼:

@model [CompanyName].Models.ResetPasswordModel 
@{ 
    ViewBag.Title = "ResetPassword"; 
} 

@if (Model == null) 
{ 
    <p> 
     We could not find your user account in the database. 
    </p> 
} 
else 
{ 

    <script type="text/javascript" src="../../Scripts/jquery.infieldlabel.min.js" ></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("label").inFieldLabels(); 
     }); 
    </script> 
    <h2> 
     Reset Password</h2> 
    <p> 
    Please enter your new password below. 
    </p> 
    <p> 
     Note: New passwords are required to be a minimum of 7 characters in length. 
    </p> 

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

    using (Html.BeginForm()) 
    { 
    <div style="position: relative;"> 
     <fieldset> 
      <legend>Reset Password</legend> 
     <label for="NewPassword" style="position:absolute; top: 24px; left: 16px;">New Password</label> 



      <div class="editor-field"> 
       @Html.PasswordFor(m => m.NewPassword) 
       @Html.ValidationMessageFor(m => m.NewPassword) 
      </div> 
      <br /> 
     <label for="ConfirmPassword" style="position:absolute; top: 64px; left: 16px;">Confirm New Password</label>  


      <div class="editor-field"> 
       @Html.PasswordFor(m => m.ConfirmPassword) 
       @Html.ValidationMessageFor(m => m.ConfirmPassword) 
      </div> 
      <p> 
       <input type="submit" value="reset Password" /> 
      </p> 

     </fieldset> 

    </div> 
    } 
} 

更新型號代碼:

[Required] 
     [DataType(DataType.Password)] 
     [Display(Name = "New password")] 
     [StringLength(50, MinimumLength = 7, ErrorMessage="New passwords must be a minimum of 7 characters, please try a different password.")] 
     public string NewPassword { get; set; } 

     [DataType(DataType.Password)] 
     [Display(Name = "Confirm new password")] 
     [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")] 
     public string ConfirmPassword { get; set; } 

控制器代碼:

public ActionResult ResetPassword(Guid secureID) 
     { 
      int id = secureID.FromSecureID(); 
      var model = new ResetPasswordModel(); 

      return View(model); 
     } 

     [HttpPost] 
     public ActionResult ResetPassword(Guid secureID, ResetPasswordModel model) 
     { 

      if (ModelState.IsValid) 
      { 
       int id = secureID.FromSecureID(); 
       var user = Database.Users.FirstOrDefault(u => u.ID == id); 
       if (user == null) 
       { 
        ModelState.AddModelError("ID", "Sorry! We could not find your user name in the database, please try again."); 
        return View(model); 
       } 
       //else (model.NewPassword == null) { 
       //return View(); 
       //} 
       user.PasswordHash = model.NewPassword.ToSha1Hash(); 
       Database.SubmitChanges(); 

      } 
      return RedirectToAction("ChangePasswordSuccess"); 
     } 

更新控制器代碼:

[HttpPost] 
     public ActionResult ResetPassword(Guid secureID, ResetPasswordModel model) 
     { 

      if(ModelState.IsValid) 
       { 
       int id = secureID.FromSecureID(); 
       var user = Database.Users.FirstOrDefault(u => u.ID == id); 
       if (user == null) 
       { 
        ModelState.AddModelError("ID", "Sorry! We could not find your user name in the database, please try again."); 
        return View(model); 
       } 
       //else (model.NewPassword == null) { 
       //return View(); 
       //} 
       user.PasswordHash = model.NewPassword.ToSha1Hash(); 
       Database.SubmitChanges(); 
       return RedirectToAction("ChangePasswordSuccess"); 
      } 

      return View(model); 
     } 

更新型號代碼:

namespace [CompanyName].Models 
{ 
    public class ResetPasswordModel 
    { 
     [Required] 
     [DataType(DataType.Password)] 
     [Display(Name = "New Password")] 
     [StringLength(100, ErrorMessage = "The new must be at least 7 characters long.", MinimumLength = 7)] 
     public string Password { set; get; } 

     [Required] 
     [DataType(DataType.Password)] 
     [Compare("Password")] 
     [Display(Name = "Confirm New Password")] 
     public string ConfirmPassword { set; get; } 
    } 
} 
+1

你在演出前檢查控制器動作模型的有效性密碼更改邏輯?.. if(ModelState.IsValid)...'? – 2012-03-28 23:50:39

+0

我以爲'SqlMembershipProvider'有這個選項...他們不符合你的要求嗎? – 2012-03-28 23:50:43

+0

@Quintin是的,我是。 – Blake 2012-03-29 00:07:53

回答

1

這個屬性在你的視圖模型將照顧它。

[Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "New Password")] 
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 7)] 
    public string NewPassword{ set; get; } 

    [Required] 
    [DataType(DataType.Password)] 
    [Compare("Password")] 
    [Display(Name = "Confirm New Password")] 
    public string ConfirmPassword { set; get; } 

,並在你的控制器動作,你可以檢查ModelState.IsValid屬性,看是否驗證失敗或不

[HttpPost] 
public ActionResult ResetPassword(Guid secureID, ResetPasswordModel model) 
{ 
    if(ModelState.IsValid) 
    { 
     // Validation correct, Lets save the new password and redirect to another action 
    } 
    return View(model); 
} 
+0

好吧,我試過了,我會在一秒鐘內更新我的問題中的代碼。它似乎仍然沒有工作,並且在控制器中引用NewPassword,它實際上將新密碼寫入數據庫... – Blake 2012-03-29 00:31:46

+0

@Blake ModelState.ISValid屬性是否爲false?你是否使用斷點並查看斷點? – Shyju 2012-03-29 00:36:13

+0

我會,但我不能用斷點調試它,直到我修復錯誤,並且它仍然在我嘗試調試時在控制器中引用NewPassword時突然斷開。 – Blake 2012-03-29 00:40:17

2

我不知道什麼是ValidatePasswordLength,但System.ComponentModel.DataAnnotations.StringLengthAttribute應該足夠了,並且通過MVC粘合劑和驗證處理方式(也也在jQuery.validate方面)。

[StringLength(50, MinimumLength = 7)] 
public string NewPassword { get; set; } 

然後在你的控制器,你可以有一個這樣的動作:

public ActionResult ResetPassword(ResetPasswordViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     //do something with the valid model and return 
    } 

    return View(model); 
} 
+0

我改變了你的模型,但它仍然沒有做任何事情,當輸入密碼時......我編輯了我的問題併發布了新的代碼。 – Blake 2012-03-29 00:06:59

+2

聽起來不錯。是否與ModelState.IsValid檢查一起工作? – HackedByChinese 2012-03-29 00:07:46

+0

不,它還沒有工作,我會再次更新我的問題,並從控制器發佈代碼。 – Blake 2012-03-29 00:12:07