4.MVC各层直接的关系
=================分布登录效果2-2=============================
Login.cshtml
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Login</title> <style type="text/css"> #divMsg { display:none; border:1px solid #00ffff; height:15px; width:150px; } </style> @Scripts.Render("~/mvcAjax") <!--合并后的js文件--> <script type="text/javascript"> //Success成功 //(jsonDAta)成功后返回的相应报文 //1.Success(jsonDAta)名称与 OnSuccess = "Success"是同名的。成功后执行Success(jsonDAta)方法参数; function Success(jsonDAta) { alert(jsonDAta) } </script> </head> <body> @using (Ajax.BeginForm(new AjaxOptions() { //Success:加载成功! LoadingElementId(提示):设置属性id HttpMethod = "post", OnSuccess = "Success",//当成功后 LoadingElementId = "divMsg"//属性显示:在加载中~~ })) { <input type="text" value="admin" name="txtName" /> <input type="password" value="123123" name="txtPwd" /> <input type="submit" value="登录" /> } <div id="divMsg">在加载中~~</div> </body> </html>
using MVCOA.Helper; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mvc; namespace MVCOA.Login.Admin { /// <summary> /// 管理员登录相关业务 /// </summary> public class AdminController:Controller { #region 1.0 管理员登录界面+ActionResult Login() /// <summary> /// 管理员登录界面 /// </summary> /// <returns></returns> [HttpGet] public ActionResult Login() { return View(); } #endregion #region 2.0 管理员登录界面+ActionResult Login() /// <summary> /// 管理员登录界面 /// </summary> /// <returns></returns> [HttpPost] public ActionResult Login(FormCollection form) { MODEL.FormatModel.AjaxMsgModel ajaxM = new MODEL.FormatModel.AjaxMsgModel() { Statu = "err", Msg = "失败~~!" }; //1.1获取数据 string strName = form["txtName"]; string strPwd = form["txtPwd"]; MODEL.Ou_UserInfo user = OperateContext.BLLSession.IOu_UserInfoBLL.GetListBy(u => u.uLoginName == strName).First(); //查询获取一个 if (user != null && user.uPwd == System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strPwd, "md5")) //加密 { return Content("ok"); } return Content("no"); } #endregion } }
效果图 :单机登录 跳出ok

============= 扩展登录ajax方式:(成功登录)效果2-3===============================================
1.IBLL程序集/IBLLExtention文件/扩展IOu_UserInfoBLL接口代码展示:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IBLL { public partial interface IOu_UserInfoBLL { MODEL.Ou_UserInfo Login(string strName, string strPwd); } }
2.BLLA/BLLExtention文件/Ou_UserInfo
BLLA程序集接口(是实现层),Ou_UserInfo类:IBLL.IOu.UserInfoBLL接口
代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BLLA { public partial class Ou_UserInfo:IBLL.IOu_UserInfoBLL { public MODEL.Ou_UserInfo Login(string strName, string strPwd) { MODEL.Ou_UserInfo usr = base.GetListBy(u => u.uLoginName==strName).FirstOrDefault(); if (usr != null && usr.uPwd == Common.DBhelperD5.MD5(strPwd)) //MD5加密 &&并且 { return usr; } return null; } } }
MVCOA.Login.Admin
AdminController.cs 分布登录控制器代码
using MVCOA.Helper; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; namespace MVCOA.Login.Admin { /// <summary> /// 管理员登陆等相关业务 /// </summary> public class AdminController : Controller { #region 1.0 管理员登陆页面 +ActionResult Login() /// <summary> /// 1.0 管理员登陆页面 /// </summary> /// <returns></returns> [HttpGet] public ActionResult Login() { return View(); } #endregion #region 1.0 管理员登陆页面 +ActionResult Login() /// <summary> /// 1.0 管理员登陆页面 /// </summary> /// <returns></returns> [HttpPost] public ActionResult Login(FormCollection form) { MODEL.FormatModel.AjaxMsgModel ajaxM = new MODEL.FormatModel.AjaxMsgModel() { Statu = "err", Msg = "失败~~!" };//统一ajax格式 //1.1获取数据 string strName = form["txtName"]; string strPwd = form["txtPwd"]; //1.2验证(自己做) //1.3通过操作上下文获取 用户业务接口对象,调用里面的登陆方法! //BLLSession//数据仓储 MODEL.Ou_UserInfo usr = OperateContext.BLLSession.IOu_UserInfoBLL.Login(strName, strPwd);//扩展登录Login if (usr != null) { ajaxM.Statu = "ok";//Statu状态 ajaxM.Msg = "登陆成功~";//Msg消息 ajaxM.BackUrl = "/admin/admin/index"; } return Json(ajaxM); } #endregion public ActionResult Index() { return View(); } } }
Login.cshtml 视图
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Login</title> @*<script type="text/javascript" src="~/mvcAjax"></script>*@ <style type="text/css"> #divMsg { display: none; /*visibility:hidden;*/ } </style> @Scripts.Render("/mvcAjax") <script type="text/javascript" src="~/Scripts/jquery.msgProcess.js"></script> <script type="text/javascript"> function Success(jsonData) { $.procAjaxData(jsonData, function () { window.location = jsonData.BackUrl; }); } </script> </head> <body> @using (Ajax.BeginForm(new AjaxOptions() { HttpMethod = "post", OnSuccess = "Success", LoadingElementId = "divMsg" })) { <input type="text" value="admin" name="txtName" /> <input type="password" value="123123" name="txtPwd" /> <input type="submit" value="登陆" /> } <div id="divMsg">加载中~~~</div> </body> </html>
运行效果: 确定后跳到index页面


=========== ==== MD5加密 / 票据加密 /票据解密 ,调用票据加密用户id ==================================
1.MD5加密
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Common { public static class DBhelperD5 { public static string MD5(string str) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5"); } } }
2.使用 票据对象 加密:EncryptUserInfo
3.使用票据字符解密:DecryptUserInfo
namespace Common { /// <summary> /// 360 安全助手 /// </summary> public class SecurityHelper { #region 1.0 使用 票据对象 将 用户数据 加密成字符串 +string EncryptUserInfo(string userInfo) /// <summary> /// 使用 票据对象 将 用户数据 加密成字符串 /// </summary> /// <param name="userInfo"></param> /// <returns></returns> public static string EncryptUserInfo(string userInfo) { //1.1 将用户数据 存入 票据对象 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "哈哈", DateTime.Now, DateTime.Now, true, userInfo); //1.2 将票据对象 加密成字符串(可逆) string strData = FormsAuthentication.Encrypt(ticket); return strData; } #endregion #region 2.0 加密字符串 解密 +string DecryptUserInfo(string cryptograph) /// <summary> /// 加密字符串 解密 /// </summary> /// <param name="cryptograph">加密字符串</param> /// <returns></returns> public static string DecryptUserInfo(string cryptograph) { //1.1 将 加密字符串 解密成 票据对象 FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cryptograph); //1.2 将票据里的 用户数据 返回 return ticket.UserData; } #endregion }
//调用票据:加密用户id
[HttpPost] public ActionResult Login(FormCollection form) { MODEL.FormatModel.AjaxMsgModel ajaxM = new MODEL.FormatModel.AjaxMsgModel() { Statu = "err", Msg = "失败~~!" }; //1.1获取数据 string strName = form["txtName"]; string strPwd = form["txtPwd"]; //1.2验证(自己做) //1.3通过操作上下文获取 用户业务接口对象,调用里面的登陆方法! //BLLSession//数据仓储 MODEL.Ou_UserInfo usr = OperateContext.BLLSession.IOu_UserInfoBLL.Login(strName, strPwd);//扩展登录Login if (usr != null) { //2.1保存 用户数据(Session or Coookie) if (!string.IsNullOrEmpty(form["isAllway"]))//如果选择了复选框,则使用cookie保存数据,要加密 { //2.1.2将用户id加密成字符串 string strCookieValue= Common.SecurityHelper.DecryptUserInfo(usr.uId.ToString());//调用票据加密 } //2.2查询当前用户的权限,并将权限存入 Session中 ajaxM.Statu = "ok";//Statu状态 ajaxM.Msg = "登陆成功~";//Msg消息 ajaxM.BackUrl = "/admin/admin/index"; //ajaxM.BackUrl = "/home/index"; } return Json(ajaxM); } #endregion
=========================统一ajax方式==========================================================
1.统一的 Ajax格式类
/// <summary> /// 统一的 Ajax格式类 /// </summary> public class AjaxMsgModel { public string Msg { get; set; } public string Statu { get; set; } public string BackUrl { get; set; } public object Data { get; set; }//数据对象 }
2.js方法
//procAjaxData:方法名,data:参数 (function ($) { $.extend($, { procAjaxData: function (data,funcSuc,funcErr) { if (!data.Statu) { return; } switch (data.Statu) { case "ok": alert("OK:" + data.Msg); if (funcSuc) funcSuc(data); break; case "err": alert("ERR:" + data.Msg); if (funcErr) funcErr(data); break; } } }); }(jQuery));
3. 使用ajax格式类
MODEL.FormatModel.AjaxMsgModel ajaxM = new MODEL.FormatModel.AjaxMsgModel() { Statu = "err", Msg = "失败~~!" };
代码:
[HttpPost] public ActionResult Login(FormCollection form) { MODEL.FormatModel.AjaxMsgModel ajaxM = new MODEL.FormatModel.AjaxMsgModel() { Statu = "err", Msg = "失败~~!" }; //1.1获取数据 string strName = form["txtName"]; string strPwd = form["txtPwd"]; //1.2验证(自己做) //1.3通过操作上下文获取 用户业务接口对象,调用里面的登陆方法! //BLLSession//数据仓储 MODEL.Ou_UserInfo usr = OperateContext.BLLSession.IOu_UserInfoBLL.Login(strName, strPwd);//扩展登录Login if (usr != null) { //2.1保存 用户数据(Session or Coookie) if (!string.IsNullOrEmpty(form["isAllway"]))//如果选择了复选框,则使用cookie保存数据,要加密 {
//2.1.2将用户id加密成字符串
string strCookieValue= Common.SecurityHelper.DecryptUserInfo(usr.uId.ToString());//调用票据加密,将用户id加密
} //2.2查询当前用户的权限,并将权限存入 Session中 ajaxM.Statu = "ok";//Statu状态 ajaxM.Msg = "登陆成功~";//Msg消息 ajaxM.BackUrl = "/admin/admin/index"; //ajaxM.BackUrl = "/home/index"; } return Json(ajaxM); }
==========================登录保存session,cookie====================
===========用户id保存到Cookie====================
1.登录选择复习框(打勾),存入Cookie对象。2.使用(票据加密) 然后存入Cookie,跳转到index页

1.Login.cshtml
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Login</title> @*<script type="text/javascript" src="~/mvcAjax"></script>*@ <style type="text/css"> #divMsg { display: none; /*visibility:hidden;*/ } </style> @Scripts.Render("/mvcAjax") <script type="text/javascript" src="~/Scripts/jquery.msgProcess.js"></script> <script type="text/javascript"> function Success(jsonData) { $.procAjaxData(jsonData, function () { window.location = jsonData.BackUrl; }); } </script> </head> <body> @using (Ajax.BeginForm(new AjaxOptions() { HttpMethod = "post", OnSuccess = "Success", LoadingElementId = "divMsg" })) { <input type="text" value="admin" name="txtName" /> <input type="password" value="123123" name="txtPwd" /> <input type="checkbox" name="isAllway" value="1" /> <input type="submit" value="登陆" /> } <div id="divMsg">加载中~~~</div> </body> </html>
admin控制器
using MVCOA.Helper; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; namespace MVCOA.Login.Admin { /// <summary> /// 管理员登陆等相关业务 /// </summary> public class AdminController : Controller { #region 1.0 管理员登陆页面 +ActionResult Login() /// <summary> /// 1.0 管理员登陆页面 /// </summary> /// <returns></returns> [HttpGet] public ActionResult Login() { return View(); } #endregion #region 1.0 管理员登陆页面 +ActionResult Login() /// <summary> /// 1.0 管理员登陆页面 /// </summary> /// <returns></returns> [HttpPost] public ActionResult Login(FormCollection form) { MODEL.FormatModel.AjaxMsgModel ajaxM = new MODEL.FormatModel.AjaxMsgModel() { Statu = "err", Msg = "失败~~!" }; //1.1获取数据 string strName = form["txtName"]; string strPwd = form["txtPwd"]; //1.2验证(自己做) //1.3通过操作上下文获取 用户业务接口对象,调用里面的登陆方法! //BLLSession//数据仓储 MODEL.Ou_UserInfo usr = OperateContext.BLLSession.IOu_UserInfoBLL.Login(strName, strPwd);//扩展登录Login if (usr != null) { //2.1保存 用户数据(Session or Coookie) if (!string.IsNullOrEmpty(form["isAllway"]))//如果选择了复选框,则使用cookie保存数据,要加密 { //2.1.2将用户id加密成字符串 string strCookieValue= Common.SecurityHelper.EncryptUserInfo(usr.uId.ToString());//调用票据加密 //2.2查询当前用户的权限,并将权限存入 Session中 HttpCookie cookie = new HttpCookie("aiafo2", strCookieValue);//用户id存入Cookie cookie.Path = "/admin/"; cookie.Expires = DateTime.Now.AddDays(1);//失效时间是一天 Response.Cookies.Add(cookie);//发回去 } ajaxM.Statu = "ok";//Statu状态 ajaxM.Msg = "登陆成功~";//Msg消息 ajaxM.BackUrl = "/admin/admin/index"; //ajaxM.BackUrl = "/home/index"; } return Json(ajaxM); } #endregion public ActionResult Index() { return View(); } } }
最后跳转到index页。
![]()
3.看看在cookie加密后的数据。
![]()
================不光存入cookie,还要存入session============================================
1.为了安装考虑:不直接存入session,因为是代理里。所以改成真正的实体对象。2.在MODEL中,新建文件夹ExtensionModel,新建类Ou_UserInfo.cs类。
using MVCOA.Helper; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; namespace MVCOA.Login.Admin { /// <summary> /// 管理员登陆等相关业务 /// </summary> public class AdminController : Controller { #region 1.0 管理员登陆页面 +ActionResult Login() /// <summary> /// 1.0 管理员登陆页面 /// </summary> /// <returns></returns> [HttpGet] public ActionResult Login() { return View(); } #endregion #region 1.0 管理员登陆页面 +ActionResult Login() /// <summary> /// 1.0 管理员登陆页面 /// </summary> /// <returns></returns> [HttpPost] public ActionResult Login(FormCollection form) { MODEL.FormatModel.AjaxMsgModel ajaxM = new MODEL.FormatModel.AjaxMsgModel() { Statu = "err", Msg = "失败~~!" }; //1.1获取数据 string strName = form["txtName"]; string strPwd = form["txtPwd"]; //1.2验证(自己做) //1.3通过操作上下文获取 用户业务接口对象,调用里面的登陆方法! //BLLSession//数据仓储 MODEL.Ou_UserInfo usr = OperateContext.BLLSession.IOu_UserInfoBLL.Login(strName, strPwd);//扩展登录Login在BLLA里(调用真正的UserInfo实体对象), if (usr != null) { //2.1保存 用户数据(Session or Coookie) //2.1.1为了安全考虑, MODEL.Ou_UserInfo代理类,UserInfo改成真正的实体。再存入Cookie Session["aiafo2"] = usr; if (!string.IsNullOrEmpty(form["isAllway"]))//如果选择了复选框,则使用cookie保存数据,要加密 { //2.1.2将用户id加密成字符串 string strCookieValue= Common.SecurityHelper.EncryptUserInfo(usr.uId.ToString());//调用票据加密 //2.2查询当前用户的权限,并将权限存入 Session中 HttpCookie cookie = new HttpCookie("aiafo2", strCookieValue);//用户id存入Cookie cookie.Path = "/admin/"; cookie.Expires = DateTime.Now.AddDays(1);//失效时间是一天 Response.Cookies.Add(cookie);//发回去 } ajaxM.Statu = "ok";//Statu状态 ajaxM.Msg = "登陆成功~";//Msg消息 ajaxM.BackUrl = "/admin/admin/index"; //ajaxM.BackUrl = "/home/index"; } return Json(ajaxM); } #endregion public ActionResult Index() { return View(); } } }
扩展Login调用真正的实体Ou_UserInfo
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BLLA { public partial class Ou_UserInfo:IBLL.IOu_UserInfoBLL { //这时:MODEL.Ou_UserInfo传回来的,是真正的实体对象 public MODEL.Ou_UserInfo Login(string strName, string strPwd) { MODEL.Ou_UserInfo usr = base.GetListBy(u => u.uLoginName == strName).Select(u =>u.ToPoCo()).First();//ToPoCo()是真正的实体类 if (usr != null && usr.uPwd == Common.DBhelperD5.MD5(strPwd)) { return usr; } return null; } } }
MODEL命名空间,后的扩展文件ExtensionModel删掉,达到不分类的效果。
Ou_UserInfo.cs(实体类)代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MODEL { /// <summary> /// 扩展用户 实体类 /// </summary> public partial class Ou_UserInfo { /// <summary> /// 生成很纯洁的实体对象 /// </summary> /// <returns></returns> public Ou_UserInfo ToPoCo() { Ou_UserInfo poco = new Ou_UserInfo() { uId = this.uId, uDepId = this.uDepId, uPwd = this.uPwd, uGender = this.uGender, uPost = this.uPost, uRemark = this.uRemark, uIsDel = this.uIsDel, uAddTime = this.uAddTime }; return poco; } } }
运行效果:


这样就完成了cookie和session存储的目的。
================================查权限===============================



浙公网安备 33010602011771号