Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit f061d32

Browse files
committed
Do not use FormattedModelValue in password editor template
- #7418 - add quirk switch to reverse this if necessary
1 parent 4866911 commit f061d32

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/Microsoft.AspNetCore.Mvc.ViewFeatures/Internal/DefaultEditorTemplates.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal
2020
public static class DefaultEditorTemplates
2121
{
2222
private const string HtmlAttributeKey = "htmlAttributes";
23+
private const string UsePasswordValue = "Switch.Microsoft.AspNetCore.Mvc.UsePasswordValue";
2324

2425
public static IHtmlContent BooleanTemplate(IHtmlHelper htmlHelper)
2526
{
@@ -312,9 +313,15 @@ public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper)
312313

313314
public static IHtmlContent PasswordTemplate(IHtmlHelper htmlHelper)
314315
{
316+
object value = null;
317+
if (AppContext.TryGetSwitch(UsePasswordValue, out var usePasswordValue) && usePasswordValue)
318+
{
319+
value = htmlHelper.ViewData.TemplateInfo.FormattedModelValue;
320+
}
321+
315322
return htmlHelper.Password(
316323
expression: null,
317-
value: htmlHelper.ViewData.TemplateInfo.FormattedModelValue,
324+
value: value,
318325
htmlAttributes: CreateHtmlAttributes(htmlHelper, "text-box single-line password"));
319326
}
320327

test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/DefaultEditorTemplatesTest.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,55 @@ public void MultilineTextTemplate_ReturnsTextArea()
521521
Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
522522
}
523523

524+
[Fact]
525+
public void PasswordTemplate_ReturnsInputElement_IgnoresValues()
526+
{
527+
// Arrange
528+
var expected = "<input class=\"HtmlEncode[[text-box single-line password]]\" " +
529+
"id=\"HtmlEncode[[FieldPrefix]]\" name=\"HtmlEncode[[FieldPrefix]]\" " +
530+
"type=\"HtmlEncode[[password]]\" />";
531+
532+
// Template ignores Model.
533+
var model = "Model string";
534+
535+
var helper = DefaultTemplatesUtilities.GetHtmlHelper(model);
536+
var viewData = helper.ViewData;
537+
var templateInfo = viewData.TemplateInfo;
538+
templateInfo.HtmlFieldPrefix = "FieldPrefix";
539+
540+
// Template ignores FormattedModelValue, ModelState and ViewData.
541+
templateInfo.FormattedModelValue = "Formatted string";
542+
viewData.ModelState.SetModelValue("FieldPrefix", "Raw model string", "Attempted model string");
543+
viewData["FieldPrefix"] = "ViewData string";
544+
545+
// Act
546+
var result = DefaultEditorTemplates.PasswordTemplate(helper);
547+
548+
// Assert
549+
Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
550+
}
551+
552+
[Fact]
553+
public void PasswordTemplate_ReturnsInputElement_UsesHtmlAttributes()
554+
{
555+
// Arrange
556+
var expected = "<input class=\"HtmlEncode[[super text-box single-line password]]\" " +
557+
"id=\"HtmlEncode[[FieldPrefix]]\" name=\"HtmlEncode[[FieldPrefix]]\" " +
558+
"type=\"HtmlEncode[[password]]\" value=\"HtmlEncode[[Html attributes string]]\" />";
559+
var helper = DefaultTemplatesUtilities.GetHtmlHelper<string>(model: null);
560+
var viewData = helper.ViewData;
561+
var templateInfo = viewData.TemplateInfo;
562+
templateInfo.HtmlFieldPrefix = "FieldPrefix";
563+
564+
viewData["htmlAttributes"] = new { @class = "super", value = "Html attributes string" };
565+
566+
// Act
567+
var result = DefaultEditorTemplates.PasswordTemplate(helper);
568+
569+
// Assert
570+
Assert.Equal(expected, HtmlContentUtilities.HtmlContentToString(result));
571+
}
572+
524573
[Theory]
525574
[MemberData(nameof(TemplateNameData))]
526575
public void Editor_CallsExpectedHtmlHelper(string templateName, string expectedResult)

0 commit comments

Comments
 (0)