Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC3 Html.PasswordFor does not populate

In my view I have the following element

  @Html.PasswordFor(model => model.Password)

This is on a screen that creates/updates user details. When I am trying to update the user this field remains blank. When I change this element to a TextBoxFor it gets the data. How do I get to populate the Password field.

like image 244
kolhapuri Avatar asked Apr 12 '11 23:04

kolhapuri


4 Answers

As described above, it is better to avoid doing this for security reason. if you still want to persist the password so that you proceed from where the current validation failed, you can use the HTML helper with html attribute parameter:

     Html.PasswordFor(x => x.Password, new { value = Model.Password})
like image 84
matmat Avatar answered Oct 25 '22 19:10

matmat


This is as designed. Passwords are not filled to prevent accidental resubmits, and to prevent the page from containing unencrypted passwords. Obviously the password was wrong to begin with if you're posting back the credentials.

In your case, you could create an extension that does input the data, or just use an HTML input of type password.

like image 27
cwharris Avatar answered Oct 25 '22 18:10

cwharris


MVC protects you from doing something like this for a reason. You shouldn't actually be able to do this because the users password should not be stored unencrypted and unhashed. If your goal is to end end up on http://plaintextoffenders.com/ though, you can do something like:

<input type="password" name="Password" id="Password" value="@Model.Password" />
like image 41
bkaid Avatar answered Oct 25 '22 20:10

bkaid


I found this workaound. I needed my password shown in the form:

@model User
@{
    @Html.Label(Model.Username, new { @class = "label" })
    @Html.TextBoxFor(Model => Model.Username, new { @class = "form-control" })

    @Html.Label(Model.Password, new { @class = "label" })
    @Html.TextBoxFor(Model => Model.Password, new { @class = "form-control make-pass" })
}

<script type="text/javascript">
    $(".make-pass").attr("type", "password");
</script>

This will make your input password-type without losing the value.

like image 35
Ilan Olkies Avatar answered Oct 25 '22 19:10

Ilan Olkies