zoukankan      html  css  js  c++  java
  • 46-Identity MVC:登录逻辑实现

    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>
                        }
  • 相关阅读:
    Coolite comboBox控件动态显示树形结构(无限树)
    WinXP 允许搜索PHP格式
    LINQ To DataSet 几个常用示例
    在Windows系统上安装PHP工作环境
    将LINQ To DataSet 传回之对象集转成DataTable的函数
    .net wsdl文件用法
    上班了,抱怨一下
    写在情人节
    快乐云南行
    单车骑天下 VS 公益旅游活动
  • 原文地址:https://www.cnblogs.com/qinzb/p/9410902.html
Copyright © 2011-2022 走看看