MVC自定义验证

原创 Laughing  2017-08-02 00:47  阅读 48 次 评论 0 条

在前面两篇文章中,我们介绍了使用Session和Forms进行用户认证的方法。具体的可以参考




前面两种方法虽然简单,但是基本没有实用的价值。因为我们存储用户信息不可能只存储用户姓名,这样基本是毫无意义的事情。这篇文章我们介绍的是自定义验证,其实就是建立一个用户模型,然后存储相关的用户信息。

自定义验证基本步骤

  1. 建立用户信息模型
  2. 序列化用户信息
  3. 创建FormsAuthenticationTicket
  4. 加密保存创建FormsAuthenticationTicket信息
  5. 创建并写入cookie信息

建立模型信息

这里为了方便演示,创建了一个简单的用户信息,存储用户姓名以及登录日期

  1. using System;  
  2. namespace SportsStore.Domain.Users  
  3. {  
  4.     public class UserInfo  
  5.     {  
  6.         public string UerName { getset; }  
  7.         public DateTime LoginTime { getset; }  
  8.     }  
  9. }  

View代码

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Index</title>  
  5. </head>  
  6. <body>  
  7.         <div class="panel">  
  8.             <div class="panel-heading">  
  9.                 Session认证  
  10.             </div>  
  11.             <div class ="panel-body">  
  12.     @if (!string.IsNullOrEmpty(ViewBag.UserName))  
  13.     {  
  14.         <form action="/home/LoginOut">  
  15.             <div class="panel panel-body">  
  16.                 用户名:<span class="label label-success">@ViewBag.UserName</span>  
  17.                 <input type="submit" class="btn btn-default" value="退出" />  
  18.             </div>  
  19.         </form>  
  20.     }  
  21.     else  
  22.     {  
  23.         <form action="/home/LoginIn">  
  24.             <div class="panel panel-body">  
  25.                 用户名:  
  26.                 <input type="text" class="text-left" id="txtUserName" name="txtUserName"/>  
  27.                 <input type="submit" class="btn btn-default" value="登录" />  
  28.             </div>  
  29.         </form>  
  30.     }  
  31.                 </div>  
  32.             </div>  
  33.   
  34.   
  35.             <div class="panel">  
  36.             <div class="panel-heading">  
  37.                 Forms认证  
  38.             </div>  
  39.             <div class ="panel-body">  
  40.     @if (!string.IsNullOrEmpty(ViewBag.UserName))  
  41.     {  
  42.         <form action="/home/LoginOut1">  
  43.             <div class="panel panel-body">  
  44.                 用户名:<span class="label label-success">@ViewBag.UserName</span>  
  45.                 <input type="submit" class="btn btn-default" value="退出" />  
  46.             </div>  
  47.         </form>  
  48.     }  
  49.     else  
  50.     {  
  51.         <form action="/home/LoginIn1">  
  52.             <div class="panel panel-body">  
  53.                 用户名:  
  54.                 <input type="text" class="text-left" id="txtUserName" name="txtUserName"/>  
  55.                 <input type="submit" class="btn btn-default" value="登录" />  
  56.             </div>  
  57.         </form>  
  58.     }  
  59.                 </div>  
  60.             </div>  
  61.   
  62.           <div class="panel">  
  63.             <div class="panel-heading">  
  64.                 自定义验证  
  65.             </div>  
  66.             <div class ="panel-body">  
  67.     @if (!string.IsNullOrEmpty(ViewBag.UserName))  
  68.     {  
  69.         <form action="/home/LoginOut2">  
  70.             <div class="panel panel-body">  
  71.                 用户名:<span class="label label-success">@ViewBag.UserName</span>  
  72.                         登录时间:<span class="label label-danger">@ViewBag.LoginTime</span>  
  73.                 <input type="submit" class="btn btn-default" value="退出" />  
  74.             </div>  
  75.         </form>  
  76.     }  
  77.     else  
  78.     {  
  79.         <form action="/home/LoginIn2">  
  80.             <div class="panel panel-body">  
  81.                 用户名:  
  82.                 <input type="text" class="text-left" id="txtUserName" name="txtUserName"/>  
  83.                 <input type="submit" class="btn btn-default" value="登录" />  
  84.             </div>  
  85.         </form>  
  86.     }  
  87.                 </div>  
  88.             </div>  
  89.           
  90. </body>  
  91. </html>  

Controller代码

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.IO;  
  7. using System.Web.Security;  
  8. using SportsStore.Domain.Users;  
  9. using Newtonsoft.Json;  
  10.   
  11. namespace SportsStore.WebUI.Controllers  
  12. {  
  13.     public class HomeController : Controller  
  14.     {  
  15.         public ActionResult Index()  
  16.         {  
  17.             ViewBag.Title = "首页";  
  18.             ViewBag.UserName = Session["UserName"]?.ToString();  
  19.             if(string.IsNullOrEmpty(ViewBag.UserName)){  
  20.                 ViewBag.UserName = User.Identity.Name;//获取cookie中存储的信息  
  21.                 var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];  
  22.                 if (cookie != null)  
  23.                 {  
  24.                     var ticket = FormsAuthentication.Decrypt(cookie.Value);  
  25.                     if (!string.IsNullOrEmpty(ticket.UserData))  
  26.                     {  
  27.                         var userInfo = JsonConvert.DeserializeObject<UserInfo>(ticket.UserData);  
  28.                         ViewBag.LoginTime = userInfo.LoginTime.ToString("F");  
  29.                     }  
  30.                     else  
  31.                     {  
  32.                         string role = ticket.Name;  
  33.                     }  
  34.                 }  
  35.             }  
  36.             return View();  
  37.         }  
  38.   
  39.         public void LoginIn(string txtUserName)  
  40.         {  
  41.             var aa = Request["txtUserName"];  
  42.             if (!string.IsNullOrEmpty(txtUserName))  
  43.             {  
  44.                 Session["UserName"] = txtUserName;  
  45.                 Response.Redirect(Request.UrlReferrer.LocalPath);  
  46.             }  
  47.             else  
  48.             {  
  49.                 Session["UserName"] = null;  
  50.             }  
  51.   
  52.         }  
  53.   
  54.         public void LoginOut()  
  55.         {  
  56.             Session["userName"] = null;  
  57.             Response.Redirect(Request.UrlReferrer.LocalPath);//重定向到原来页面  
  58.         }  
  59.   
  60.         public void LoginIn1(){  
  61.             string txtUserName = Request["txtUserName"]?.ToString();  
  62.             if(!string.IsNullOrEmpty(txtUserName)){  
  63.                 FormsAuthentication.SetAuthCookie(txtUserName,true);  
  64.                 Response.Redirect(Request.UrlReferrer.LocalPath);  
  65.             }else{  
  66.                 FormsAuthentication.SignOut();  
  67.             }  
  68.         }  
  69.   
  70.         public void LoginOut1(){  
  71.             FormsAuthentication.SignOut();  
  72.             Response.Redirect(Request.UrlReferrer.LocalPath);  
  73.         }  
  74.   
  75.         public void LoginIn2(string txtUserName)  
  76.         {  
  77.             if (!string.IsNullOrEmpty(txtUserName))  
  78.             {  
  79.                 UserInfo userInfo = new UserInfo  
  80.                 {  
  81.                     UerName = txtUserName,  
  82.                     LoginTime = DateTime.Now  
  83.                 };  
  84.                 //1.序列化用户信息  
  85.                 var data = JsonConvert.SerializeObject(userInfo);  
  86.                 //2.创建FormsAuthenticationTicket  
  87.                 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, txtUserName, DateTime.Now, DateTime.Now.AddDays(1), true, data);  
  88.                 //3.加密保存  
  89.                 string cookitValue = FormsAuthentication.Encrypt(ticket);  
  90.                 //4.创建登录cookie  
  91.                 HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookitValue);  
  92.                 cookie.HttpOnly = true;  
  93.                 cookie.Secure = FormsAuthentication.RequireSSL;  
  94.                 cookie.Domain = FormsAuthentication.CookieDomain;  
  95.                 cookie.Path = FormsAuthentication.FormsCookiePath;  
  96.                 Response.Cookies.Remove(cookie.Name);  
  97.                 Response.Cookies.Add(cookie);  
  98.                 Response.Redirect(Request.UrlReferrer.LocalPath);  
  99.             }  
  100.         }  
  101.     }  
  102. }  

[/code]

登录界面展示

历史上的今天:

本文地址:https://www.lisen.me/mvc-custom-validation.html
版权声明:本文为原创文章,版权归 木子网 所有,欢迎分享本文,转载请保留出处!

发表评论


表情