Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I check to make sure a new password is a set length with C#?
    text
    copied!<p>I'm working on a password reset function and trying to check to make sure the new password is at least 7 characters. It will run and pass the new password to the controller and set it as the password for the user but it just uses whatever was entered instead of checking to make sure it meets the password requirements. Thanks for any suggestions :)</p> <p>Here's the model: </p> <pre><code>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; } } } </code></pre> <p>And here's the page to reset the password:</p> <pre><code>@model [CompanyName].Models.ResetPasswordModel @{ ViewBag.Title = "ResetPassword"; } @if (Model == null) { &lt;p&gt; We could not find your user account in the database. &lt;/p&gt; } else { &lt;script type="text/javascript" src="../../Scripts/jquery.infieldlabel.min.js" &gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function () { $("label").inFieldLabels(); }); &lt;/script&gt; &lt;h2&gt; Reset Password&lt;/h2&gt; &lt;p&gt; Please enter your new password below. &lt;/p&gt; &lt;p&gt; Note: New passwords are required to be a minimum of 7 characters in length. &lt;/p&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; using (Html.BeginForm()) { &lt;div style="position: relative;"&gt; &lt;fieldset&gt; &lt;legend&gt;Reset Password&lt;/legend&gt; &lt;label for="NewPassword" style="position:absolute; top: 24px; left: 16px;"&gt;New Password&lt;/label&gt; &lt;div class="editor-field"&gt; @Html.PasswordFor(m =&gt; m.NewPassword) @Html.ValidationMessageFor(m =&gt; m.NewPassword) &lt;/div&gt; &lt;br /&gt; &lt;label for="ConfirmPassword" style="position:absolute; top: 64px; left: 16px;"&gt;Confirm New Password&lt;/label&gt; &lt;div class="editor-field"&gt; @Html.PasswordFor(m =&gt; m.ConfirmPassword) @Html.ValidationMessageFor(m =&gt; m.ConfirmPassword) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="reset Password" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;/div&gt; } } </code></pre> <p>Updated model code: </p> <pre><code>[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; } </code></pre> <p>Controller code:</p> <pre><code>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 =&gt; 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"); } </code></pre> <p>Updated controller code:</p> <pre><code>[HttpPost] public ActionResult ResetPassword(Guid secureID, ResetPasswordModel model) { if(ModelState.IsValid) { int id = secureID.FromSecureID(); var user = Database.Users.FirstOrDefault(u =&gt; 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); } </code></pre> <p>Updated model code:</p> <pre><code>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; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload