c# - 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>
解决方案
基本上在您的视图中使用您的模型和 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
会将内部数据传递给控制器。
推荐阅读
- javascript - 动态创建 web 组件
- python - 如何在循环中删除 '\r' 以读取 python 中的文件
- macos - MacOS - NSWindowController 在导出后未加载 NSViewController
- c++ - 如何重载 std::cout << std::endl?
- python - 如何使 .counts 返回 2 个计数而不是 1
- java - SignalR Java 客户端是否支持 TLS 1.2?
- sql - 对具有 where 条件的一对多表使用连接的 SQL 查询
- c# - 如何仅获取从 Roslyn 提取的 C# 解析器(语法树)
- haskell - 单子的第一次体验(Haskell)
- javascript - 触发点击保存到驱动器按钮