Asp. Net MVC 5 simple registration and login using Identity, mvcidentity
There are many login and registration methods for. Net MVC 5, but Identity implementation may be simpler and easier to understand.
Create a project
Select Empty and MVC as follows
Then open the NuGet Package Manager to install several packages separately.
Then add the ApplicationUser class, SignInModel class, SignUpModel class, And ApplicationDbContext class to the Models folder. Of course, you can also assign the ApplicationDbContext class to another class library for demonstration, hierarchy is not so clear
----------------------------------------------------------------------
ApplicationUser class
ApplicationDbContext class
SignInModel class
SignUpModel class
Add the ApplicationSignInManager class, ApplicationUserManager class, And ApplicationUserStore class to the App_Start folder.
---------------------------------------------------------------------
ApplicationUserManager class
ApplicationSignInManager class
ApplicationUserStore class
Add the HomeController Controller and AccountController Controller to the Controller folder.
First add the index View to the HomeController Controller
Index view code
@using Microsoft.AspNet.Identity@{ ViewBag.Title = "Index";}
Then the AccountController controller code
Private ApplicationSignInManager signInManager; private ApplicationUserManager userManager; public ApplicationSignInManager SignInManager {get {return signInManager ?? HttpContext. GetOwinContext (). Get <ApplicationSignInManager> () ;}private set {signInManager = value ;}} public ApplicationUserManager UserManager {get {return userManager ?? HttpContext. getOwinContext (). getUserManager <ApplicationUserManager> ();} private set {userManager = value;} [AllowAnonymous] public ActionResult Login (string returnUrl) {ViewBag. returnUrl = returnUrl; return View ();} [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task <ActionResult> Login (SignInModel model, string returnUrl) {if (! ModelState. isValid) {return View (model);} var result = await SignInManager. passwordSignInAsync (model. email, model. password, model. rememberMe, shouldLockout: false); switch (result) {case SignInStatus. success: return RedirectToLocal (returnUrl); case SignInStatus. failure: default: ModelState. addModelError ("", "Invalid Login"); return View (model) ;}} [AllowAnonymous] public ActionResult Register () {return View ();} [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task <ActionResult> Register (SignUpModel model) {if (ModelState. isValid) {var user = new ApplicationUser {UserName = model. email, Email = model. email}; var result = await UserManager. createAsync (user, model. password); if (result. succeeded) {await SignInManager. signInAsync (user, isPersistent: false, rememberBrowser: false); return RedirectToAction ("Index", "Home") ;}adderrors (result) ;}return View (model );} [HttpPost] [ValidateAntiForgeryToken] public ActionResult LogOff () {AuthenticationManager. signOut (); return RedirectToAction ("Index", "Home");} private void AddErrors (IdentityResult result) {foreach (var error in result. errors) {ModelState. addModelError ("", error) ;}} private ActionResult RedirectToLocal (string returnUrl) {if (Url. isLocalUrl (returnUrl) {return Redirect (returnUrl);} return RedirectToAction ("Index", "Home");} private IAuthenticationManager AuthenticationManager {get {return HttpContext. getOwinContext (). authentication ;}}
Then add and generate the Login and Register pages respectively.
Login Page code
@ Model IdentityDemo. models. signInModel @ {ViewBag. title = "Login" ;}< h2> Login
Register Page code
@model IdentityDemo.Models.SignUpModel@{ ViewBag.Title = "Register";}
Add the Startup class to the root directory of the project.
Then modify the Web. Config file in the root directory.
Finally, let's test the effect, as shown in figure