MVC5 website development practice 2.1, administrator login, mvc52.1

Source: Internet
Author: User

MVC5 website development practice 2.1, administrator login, mvc52.1
Directory

Overview of MVC5 website development practices

MVC5 website development practices 1. Create a project

MVC5 website development practices 2. Background Management

 

1. Create a SHA256 encryption method.

Add the folder Security in the Data Project, add the class Encryption to the folder, and write a SHA256 Encryption method in the class.

Using System; using System. security. cryptography; using System. text; namespace Ninesky. data. security {// <summary> /// Encryption Class /// <remarks> /// create: 2014.12.13 // </remarks> /// </summary> public class Encryption {/// <summary> // 256-bit hash Encryption /// </summary> /// <param name = "plainText"> plainText </param> /// <returns> ciphertext </returns> public static string Sha256 (string plainText) {SHA256Managed _ sha256 = new SHA256Managed (); byte [] _ cipherText = _ sha256.ComputeHash (Encoding. default. getBytes (plainText); return Convert. toBase64String (_ cipherText );}}}

 

2. Add a reference to the Data Project for the Website project. 3. Add a logon view Model

Add the view model class LoginViewModel to the Models folder in the Config area. The Code is as follows:

Using System. componentModel. dataAnnotations; namespace Ninesky. website. areas. config. models {/// <summary> /// login model /// <remarks> /// create: 2014.12.13 // </remarks> /// </summary> public class LoginViewModel {[StringLength (20, MinimumLength = 2, errorMessage = "{0} {2}-{1} characters")] [Display (Name = "Account")] public string Account {get; set ;} [StringLength (20, MinimumLength = 4, ErrorMessage = "{0} length {2}-{1} characters")] [Display (Name = "password")] public string Password {get; set ;}}}
4. Add an administrator controller.

Right-click the Controller folder in config and choose create Controller [MVC5 Controller-null] From the shortcut menu. Enter "AdministratorController" in the name of the Controller.

Add a Login action and a Login action in HttpPost mode.

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; using Ninesky. user; using Ninesky. data. security; namespace Ninesky. website. areas. config. controllers {// <summary> // administrator controller /// <remarks> /// create: 2014.12.13 // </remarks> /// </summary> public class AdministratorController: Controller {private AdministratorService _ adminService = new Administrato RService (ContextFactory. getCurrentContext (); // log on to the public ActionResult Login () {return View ();} [ValidateAntiForgeryToken ()] [HttpPost] public ActionResult Login (Models. loginViewModel loginVM) {if (ModelState. isValid) {var _ admin = _ adminService. find (a =>. account = loginVM. account); if (_ admin = null) ModelState. addModelError ("Account", "Account does not exist"); else if (Encryption. sha256 (loginVM. passwor D )! = _ Admin. password) ModelState. addModelError ("Password", "Incorrect Password"); else {_ admin. loginTime = System. dateTime. now; _ admin. loginIp = Request. userHostAddress; _ adminService. update (_ admin); Session. add ("Account", loginVM. account); Session. add ("Password", _ admin. password); return RedirectToAction ("Index", "Home") ;}} return View (loginVM );}}}

HttpPost actions first verify whether the model is correct, then verify whether the user exists and the password is correct, then update the Administrator Logon Time and ip address, and save the account and password to the session, go to the Home/index page.

5. Add a view.

Right-click "Login" action to add a view.

Modify the code and add a reference to bootstrap. The Code is as follows:

6. Enable migration.

On the package management console, enter the "Enable-Migrations" command to Enable migration.

Open "Configuration. cs" in the Migrations folder of the Website project and change "AutomaticMigrationsEnabled = false;" to "AutomaticMigrationsEnabled = true ;"

7. Create a database

Enter the "Update-Database" command in the package management console again to create a Database. Then, in the server resource manager, view the created

8. Add an Administrator Account

The data in the administrator table of the database. Then add an administrator, Account "admin", password "jGl25bVBBBW96Qi9Te4V37Fnqchz/Eu4qB9vKrRIqRg ="

9. Test logon.

Open the logon view in the browser, and enter "admin" for both the account and password. You can perform a test to change the account and password.

 

======================================

Code: http://pan.baidu.com/s/1mgtBbxI

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.