Asp. Net MVC 5 simple registration and login using Identity, mvcidentity

Source: Internet
Author: User
Tags actionlink

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.