首页 > 解决方案 > MVC C# 创建用户名密码

问题描述

我想创建包含用户名密码身份验证的 MVC 项目。但我不知道我该怎么做。我不会使用数据库。只需使用 getter setter 即可。我也会在模型课上这样做。我的吸气剂二传手在里面。我还必须预定义一些用户名密码。我将用户名密码写在文本中,并将其添加到问题中。谢谢您的任何想法

    public class Response
{   
    [Required(ErrorMessage = "Please enter your username")]
    public string Username { get; set; }

    [Required(ErrorMessage = "Please enter your password")]
    public string Password { get; set; }

}

    <form asp-action="Index" method="post">
    <div asp-validation-summary="All"></div>
    <p>
        <label asp-for="Username">Username : </label>
        <input class="form-control" asp-for="Username" />
    </p>

    <p>
        <label asp-for="Password">Password : </label>
        <input class="form-control" asp-for="Password" type="password" />
    </p>

    <button class="submit" type="submit">Login</button>
</form>

标签: c#authenticationloginasp.net-core-mvc

解决方案


基本上在您的视图中使用您的模型和 Html 助手:

@model Response

@using (Html.BeginForm("Index", "ControllerName", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationFor(m=>m.Username)
    @Html.TextBoxFor(m=>m.UserName)
   <button class="submit" type="submit">Login</button>
}

这样,当您提交表单时,Response会将内部数据传递给控制器​​。


推荐阅读