46-Identity MVC:登录逻辑实现
2024-10-21 07:36:10
1- Login.cshtml
<h3>Login</h3>
@model MvcCookieAuthSample.ViewModel.LoginViewModel
<div class="row">
<div class="col-md-4">
<form method="post" asp-controller="Account" asp-action="Login"> <hr /> <div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control" />
</div> <div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" />
</div> <button type="submit" class="btn btn-default">Login</button>
</form>
</div>
</div>
2-登陆具体方法
[HttpPost]
public async Task<IActionResult> Login(ViewModel.LoginViewModel loginModel)
{
var findUser =await _userManager.FindByEmailAsync(loginModel.Email);
if (findUser == null)
{
return BadRequest();
}
await _signInManager.SignInAsync(findUser, true);
return RedirectToAction("Index", "Admin");
} public IActionResult LoginOut()
{
_signInManager.SignOutAsync();
return RedirectToAction("Index", "Home");
}
3-在修改公用模板
@if (User.Identity.IsAuthenticated)
{
<li> <a title="Welcome" asp-controller="Admin" asp-action="Index"> @User.Identity.Name</a> </li>
<li><a asp-controller="Account" asp-action="LoginOut">退出</a></li> }
else
{
<li><a asp-area="" asp-controller="Account" asp-action="Register">注册</a></li>
<li><a asp-area="" asp-controller="Account" asp-action="Login">登陆</a></li>
}
最新文章
- 布局TextView和EditText区别,layout_width和lay_weight区别--Android Studio
- SSH实战 &#183; JAVA发送邮件相关
- SILVERLIGHT 应急卫生模拟演练项目之childwindow
- 强势回归,Linux blk用实力证明自己并不弱!
- HibernateTemplate的一些常用方法总结
- [转]svn常用命令
- Cheatsheet: 2015 12.01 ~ 12.31
- shell 脚本实战笔记(1)--source/fork/exec的区别
- URAL 1072 Routing(最短路)
- 3. c的输入输出
- 【转】Select模型原理
- MyBatis学习笔记之resultMap
- RabbitMQ安装和配置
- python内存管理
- chm 字体修改
- 《python基础教程》笔记之 抽象
- ①bootstrap引入
- 学习 JavaScript (八) 引用类型之 Object
- Source-Based XSS Test Cases
- Servlet.service() for servlet [jsp] in context with path [/Healthy_manager] threw exception [Unable to compile class for JSP] with root cause java.lang.IllegalArgumentException: Page directive: inval