Alexandra Valkova
Alexandra Valkova

Reputation: 97

PasswordPropertyText vs DataType(DataType.Password) attribute

What exactly is the difference between these two .NET attributes:

Password property in my ViewModel that I have decorated with both attributes:

[PasswordPropertyText(true)]
[DataType(DataType.Password)]
public string Password { get; set; }

Password field in my View:

<div class="form-group">
    <label asp-for="Password" class="control-label"></label>
    <input asp-for="Password" class="form-control" />
    <span asp-validation-for="Password" class="text-danger"></span>
</div>

Indicates that an object's text representation is obscured by characters such as asterisks.

This is the definition of the PasswordPropertyTextAttribute, but it's actually the [DataType(DataType.Password)] attribute that fulfills that role, the [PasswordPropertyText(true)] attribute doesn't render the password field properly - the input tag should have a type="password" attribute instead of type="text".

HTML code generated when only [DataType(DataType.Password)] is applied:

<input class="form-control" type="password" id="Password" name="Password">

HTML code generated when only [PasswordPropertyText(true)] is applied:

<input class="form-control" type="text" id="Password" name="Password" value="">

HTML code generated when both [DataType(DataType.Password)] and [PasswordPropertyText(true)] are applied:

<input class="form-control" type="password" id="Password" name="Password">

I am currently confused whether or not I am applying the [PasswordPropertyText(true)] attribute properly, and what is its purpose after all, compared to [DataType(DataType.Password)].

Upvotes: 3

Views: 666

Answers (0)

Related Questions