【问题标题】:Visual Studio ASP.NET - Password is not hidden and it shows [duplicate]Visual Studio ASP.NET - 密码未隐藏,它显示 [重复]
【发布时间】:2019-04-17 22:30:47
【问题描述】:

您好,我不知道为什么在登录和输入我的密码时它没有隐藏,并且在输入时密码是可见的。我已经尝试了一切,但找不到解决方案。我也在使用 sql server management 和 Visual Studio。

<div class="form-horizontal">

    <hr />
    @Html.ValidationSummary(true)
    <div class="form-group">
        @Html.LabelFor(model => model.username, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.username, new { @class="form-control"})
            @Html.ValidationMessageFor(model => model.username)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.password, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.password, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.password)
        </div>
    </div>

【问题讨论】:

  • 标记是什么?
  • @hardkoded HTML
  • 我认为@hardkoded 的意思是你的标记是什么样的?这是您在此处发布的服务器端代码,而不是客户端显示的代码。你应该edit你的问题包括这个。
  • 如果您使用输入标签...然后在其中包含 type="password" 属性
  • 您似乎以明文形式存储密码。请不要那样做,有更好的方法。

标签: c# asp.net-mvc razor


【解决方案1】:

您想要专门使用 html 帮助器文本框并屏蔽文本。这是使用“密码”类型标签完成的。我继续修复了你的代码。

尝试用以下代码替换您的代码:

<div class="form-horizontal">

<hr />
@Html.ValidationSummary(true)
<div class="form-group">
    @Html.LabelFor(model => model.username, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextBoxFor(model => model.username, new { @class="form-control"})
        @Html.ValidationMessageFor(model => model.username)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.password, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextBoxFor(model => model.password, new { @class = "form-control" , @type="password"})
        @Html.ValidationMessageFor(model => model.password)
    </div>
</div>

我更改的所有内容都在这行代码中:

 @Html.TextBoxFor(model => model.username, new { @class="form-control", @type="password"})

我添加了@type="password"

原因是您希望将文本框的类型设置为密码文本框,以掩盖密码。

这是一个w3schools 链接,它向您展示了如何使用输入标签来执行此操作。

您也可以使用 MvcHtmlString Html.Password。

看起来像这样:

MvcHtmlString Html.Password(string name, object value, object htmlAttributes)

Html.Password() 方法生成一个输入密码元素 指定名称、值和 html 属性。

示例:学生模型

public class Student
{
    public int StudentId { get; set; }
    [Display(Name="Name")]
    public string StudentName { get; set; }
    public int Age { get; set; }
    public bool isNewlyEnrolled { get; set; }
    public string OnlinePassword { get; set; }
}

示例:Razor 视图中的 Html.Password()

@model Student

@Html.Password("OnlinePassword")

Here 是 Html.Password() 方法的绝佳资源。

【讨论】:

  • 也只有@Html.PasswordFor(...)。此外,您编辑了用户名文本框,而不是密码文本框。
  • 感谢 codeCaster 我也会把它包括在内。
  • 大声感谢您注意到我把它放在了错误的位置,我也修复了它。
猜你喜欢
  • 2021-12-01
  • 2018-09-12
  • 2018-01-15
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多