標籤:style blog http io ar color os sp for
目錄
MVC5 網站開發實踐 概述
MVC5 網站開發實踐 1、建立項目
MVC5 網站開發實踐 2、後台管理
1、 建立SHA256加密方法。
在Data項目中添加檔案夾【Security】,在檔案夾中添加類 【Encryption】,在類中寫一個SHA256加密方法。
using System;using System.Security.Cryptography;using System.Text;namespace Ninesky.Data.Security{ /// <summary> /// 加密類 /// <remarks> /// 建立:2014.12.13 /// </remarks> /// </summary> public class Encryption { /// <summary> /// 256位散列加密 /// </summary> /// <param name="plainText">明文</param> /// <returns>密文</returns> public static string Sha256(string plainText) { SHA256Managed _sha256 = new SHA256Managed(); byte[] _cipherText = _sha256.ComputeHash(Encoding.Default.GetBytes(plainText)); return Convert.ToBase64String(_cipherText); } }}
2、 Website項目添加對 Data項目的引用。3、添加登入視圖模型
在Config地區的Models檔案夾添加視圖模型類【LoginViewModel】,代碼如下:
using System.ComponentModel.DataAnnotations;namespace Ninesky.Website.Areas.Config.Models{ /// <summary> /// 登陸模型 /// <remarks> /// 建立:2014.12.13 /// </remarks> /// </summary> public class LoginViewModel { [StringLength(20, MinimumLength = 2, ErrorMessage = "{0}長度{2}-{1}個字元")] [Display(Name = "帳號")] public string Account { get; set; } [StringLength(20, MinimumLength = 4, ErrorMessage = "{0}長度{2}-{1}個字元")] [Display(Name = "密碼")] public string Password { get; set; } }}4、添加管理員控制器
在config的Controller檔案夾上點右鍵建立控制器【MVC5控制器-空】,名稱輸入【AdministratorController】,在控制器
添加一個【Login】action,和一個HttpPost方式的【Login】action。
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> /// 管理員控制器 /// <remarks> /// 建立:2014.12.13 /// </remarks> /// </summary> public class AdministratorController : Controller { private AdministratorService _adminService = new AdministratorService(ContextFactory.GetCurrentContext()); // 登陸 public ActionResult Login() { return View(); } [ValidateAntiForgeryToken()] [HttpPost] public ActionResult Login(Models.LoginViewModel loginVM) { if (ModelState.IsValid) { var _admin = _adminService.Find(a => a.Account == loginVM.Account); if (_admin == null) ModelState.AddModelError("Account", "帳號不存在"); else if (Encryption.Sha256(loginVM.Password) != _admin.Password) ModelState.AddModelError("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方式的action中先驗證模型是否有錯誤,然後驗證使用者是否存在和密碼是否正確,通過後更新管理員登入時間和ip,將帳號和密碼儲存到session中,然後跳轉到Home/index頁面。
5、 添加視圖。
在”Login“action上點右鍵添加視圖。
修改代碼稍作修改,添加對bootstrap的引用,代碼如下:
6、啟用遷移。
在程式包管理主控台輸入“Enable-Migrations”命令啟用遷移。
然後開啟Website項目Migrations檔案夾下的“Configuration.cs”,將建構函式中的“AutomaticMigrationsEnabled = false;”改為“AutomaticMigrationsEnabled = true;”
7、建立資料庫
再次在程式包管理主控台輸入“Update-Database”命令來建立資料庫。然後在伺服器總管中查看剛建立
8、添加一個管理員帳號
資料庫的管理員表的資料。然後在表中添加一個管理員,帳號”admin”,密碼”jGl25bVBBBW96Qi9Te4V37Fnqchz/Eu4qB9vKrRIqRg=“
9、測試登入。
在瀏覽器中開啟登入視圖,帳號和密碼都輸入”admin“,測試一下可以正常調轉。
============================
代碼:http://pan.baidu.com/s/1mgtBbxI
MVC5 網站開發實踐 2.1、管理員登陸