45-Identity MVC:注册逻辑实现
1-注册页Register.cshtml
<h3>Register</h3> @model MvcCookieAuthSample.ViewModel.RegisterViewModel <div class="row"> <div class="col-md-4"> <form method="post"> <h4>register a new account</h4> <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> <div class="form-group"> <label asp-for="ConfirmPassword"></label> <input asp-for="ConfirmPassword" class="form-control" /> </div> <button type="submit" class="btn btn-default">Register</button> </form> </div> </div>
2-注册方法
private UserManager<Models.ApplicationUser> _userManager; private SignInManager<Models.ApplicationUser> _signInManager; [HttpPost] public async Task<IActionResult> Register(ViewModel.RegisterViewModel registerViewModel) { Models.ApplicationUser applicationUser = new Models.ApplicationUser() { Email = registerViewModel.Email, UserName = registerViewModel.Email, NormalizedEmail = registerViewModel.Email }; IdentityResult result = await _userManager.CreateAsync(applicationUser, registerViewModel.Password);//注册 if (result.Succeeded) { await _signInManager.SignInAsync(applicationUser, true);//登陆 return RedirectToAction("Index", "Home"); } return View(); }