zoukankan      html  css  js  c++  java
  • mvc core2.1 Identity.EntityFramework Core 注册 (二)

    Startup.cs-> Configure

    app.UseAuthentication(); //启动验证

    Controllers->AccountController.cs 新建

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Diagnostics;
     4 using System.Linq;
     5 using System.Threading.Tasks;
     6 using Microsoft.AspNetCore.Mvc;
     7 using IdentityMvc.Models;
     8 using Microsoft.AspNetCore.Identity;
     9 using Microsoft.AspNetCore.Authorization;
    10 using IdentityMvc.Models.AccountViewModels;
    11 using Microsoft.AspNetCore.Authentication;
    12 
    13 namespace IdentityMvc.Controllers
    14 {
    15     public class AccountController : Controller
    16     {
    17         private readonly UserManager<ApplicationUser> _userManager;
    18         private readonly SignInManager<ApplicationUser> _signInManager;
    19        
    20         public AccountController(
    21             UserManager<ApplicationUser> userManager,
    22             SignInManager<ApplicationUser> signInManager)
    23         {
    24             _userManager = userManager;
    25             _signInManager = signInManager;
    26         }
    27         [TempData]
    28         public string ErrorMessage { get; set; }
    29         
    30         [HttpGet]
    31         [AllowAnonymous]
    32         public IActionResult Register(string returnUrl = null)
    33         {
    34             ViewData["ReturnUrl"] = returnUrl;
    35             return View();
    36         }
    37         [HttpPost]
    38         [AllowAnonymous]
    39         [ValidateAntiForgeryToken]
    40         public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
    41         {
    42             ViewData["ReturnUrl"] = returnUrl;
    43             if (ModelState.IsValid)
    44             {
    45                 var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
    46                 var result = await _userManager.CreateAsync(user, model.Password);
    47                 if (result.Succeeded)
    48                 {
    49 
    50                     //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
    51 
    52                     await _signInManager.SignInAsync(user, isPersistent: false);
    53                     return RedirectToLocal(returnUrl);
    54                 }
    55                 AddErrors(result);
    56             }
    57 
    58             // If we got this far, something failed, redisplay form
    59             return View(model);
    60         }
    61 
    62         private void AddErrors(IdentityResult result)
    63         {
    64             foreach (var error in result.Errors)
    65             {
    66                 ModelState.AddModelError(string.Empty, error.Description);
    67             }
    68         }
    69 
    70         private IActionResult RedirectToLocal(string returnUrl)
    71         {
    72             if (Url.IsLocalUrl(returnUrl))
    73             {
    74                 return Redirect(returnUrl);
    75             }
    76             else
    77             {
    78                 return RedirectToAction(nameof(HomeController.Index), "Home");
    79             }
    80         }
    81     }
    82 }
    View Code

    Models->AccountViewModels->RegisterViewModel.cs 新建

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel.DataAnnotations;
     4 using System.Linq;
     5 using System.Threading.Tasks;
     6 
     7 namespace IdentityMvc.Models.AccountViewModels
     8 {
     9     public class RegisterViewModel
    10     {
    11         [Required]
    12         [EmailAddress]
    13         [Display(Name = "Email")]
    14         public string Email { get; set; }
    15 
    16         [Required]
    17         [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
    18         [DataType(DataType.Password)]
    19         [Display(Name = "Password")]
    20         public string Password { get; set; }
    21 
    22         [DataType(DataType.Password)]
    23         [Display(Name = "Confirm password")]
    24         [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    25         public string ConfirmPassword { get; set; }
    26     }
    27 }
    View Code

    Views->Account->Register.cshtml 新建

     1 @model  RegisterViewModel
     2 @{
     3     ViewData["Title"] = "Register";
     4 }
     5 
     6 <h2>@ViewData["Title"]</h2>
     7 
     8 <div class="row">
     9     <div class="col-md-4">
    10         <form asp-route-returnUrl="@ViewData["ReturnUrl"]" method="post">
    11             <h4>Create a new account.</h4>
    12             <hr />
    13             <div asp-validation-summary="All" class="text-danger"></div>
    14             <div class="form-group">
    15                 <label asp-for="Email"></label>
    16                 <input asp-for="Email" class="form-control" />
    17                 <span asp-validation-for="Email" class="text-danger"></span>
    18             </div>
    19             <div class="form-group">
    20                 <label asp-for="Password"></label>
    21                 <input asp-for="Password" class="form-control" />
    22                 <span asp-validation-for="Password" class="text-danger"></span>
    23             </div>
    24             <div class="form-group">
    25                 <label asp-for="ConfirmPassword"></label>
    26                 <input asp-for="ConfirmPassword" class="form-control" />
    27                 <span asp-validation-for="ConfirmPassword" class="text-danger"></span>
    28             </div>
    29             <button type="submit" class="btn btn-default">Register</button>
    30         </form>
    31     </div>
    32 </div>
    View Code

    Views->_ViewImports.cshtml

    @using IdentityMvc.Models.AccountViewModels
    @using Microsoft.AspNetCore.Identity
  • 相关阅读:
    虚拟机配置桥接网络以及使用mobaxterm连接虚拟机
    maven依赖包问题2
    47.火狐浏览器登录过全球服务器之后,切换回本地服务器登录失败解决方法
    博客园美化
    vue拼图验证(vue-puzzle-vcode)
    vue使用axios
    vue中使用swiper
    VUE安装并配置
    vue中router-view不显示
    vue打包后空白
  • 原文地址:https://www.cnblogs.com/LiuFengH/p/9418062.html
Copyright © 2011-2022 走看看