Mister XP
Mister XP

Reputation: 63

How to mask Password in MVC ViewRazor

I need to mask Password in View in MVC Project, where i have list of users.

Model:

 [Required]
        [StringLength(50)]
        [DataType(DataType.Password)]
        [DisplayName("Password")]
        public string Password { get; set; }

I have this code in View, where i see the Passwords at the moment.

 @Html.DisplayFor(modelItem => item.Password)

How to Mask password in View?

EDIT:

If i try with @Html.PasswordFor(modelItem => item.Password) i see this:

Image1

Upvotes: 3

Views: 11393

Answers (3)

matias4454
matias4454

Reputation: 11

To create complete form which is suitable for data type attributes in model class you can use following statement:

@Html.EditorForModel()

This method is generating code for each property of model class, which is equivalent to following:

@Html.LabelFor(model => model.Name)
@Html.TextBoxFor(model => model.Name)

Upvotes: 0

BThompson
BThompson

Reputation: 350

If I understand correctly what you're trying to do, I recommend an entirely different approach. Really, there's no reason to display the correct number of dots corresponding to the number of characters in their password. Doing so is a security hole; it tells attackers that they only need to try passwords of a given length. In fact, you should not be storing passwords in plaintext to begin with, so you shouldn't know how many characters there are

All that said, I recommend using

@Html.Raw("\u2022\u2022\u2022\u2022\u2022")

The string "\u2022" refers to a unicode character for a black dot, so this will simply display 5 black dots on the line

Upvotes: 8

LiverpoolOwen
LiverpoolOwen

Reputation: 816

@Html.PasswordFor(modelItem => item.Password)

Try this

Upvotes: 4

Related Questions