码迷,mamicode.com
首页 > 其他好文 > 详细

优化EF以及登录验证

时间:2015-03-19 00:47:42      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

1 ,优化ef

 


using System.Runtime.Remoting.Messaging;//线程缓存 引用

在DAL 中 BaseDAL 中

//1.0 实例化上下文对象
/*缺点在控制器
* 在处理线程中 就会创建相同个数的ef 容器 每一次操作 都必须 savechanges
*/
// BaseDbContext db = new BaseDbContext();

public BaseDbContext db {
get {
//问线程缓存有没有
object obj = CallContext.GetData("BaseDbContext");//key 可以随意写

if (obj == null)
{
obj = new BaseDbContext();
CallContext.SetData("BaseDbContext", obj);//存入线程缓存中
}
return obj as BaseDbContext;
}
}
--------------------------------
2,登录验证 验证码

为了方便 先 自定义 一个 所有控制器类的父类BaseController 并继承Controller

在BaseController 封装统一的ajax 响应的json 方法

并自定义当前数据业务逻辑接口成员

代码如下:

#region 封装统一的ajax响应的json方法
#region 成功
protected ActionResult WriteSuccess(string msg)
{
return Json(new { status = 0, msg = msg },JsonRequestBehavior.AllowGet);
}
protected ActionResult WriteSuccess(string msg, object data)
{
return Json(new { status = 0, msg = msg, datas = data }, JsonRequestBehavior.AllowGet);
}
#endregion
#region 失败
protected ActionResult WriteError(string msg)
{
return Json(new { status = 1, msg = msg }, JsonRequestBehavior.AllowGet);
}
protected ActionResult WriteError(Exception msg)
{
return Json(new { status = 1, msg = msg.Message }, JsonRequestBehavior.AllowGet);
}
#endregion
#endregion
#region 定义当前数据业务逻辑接口成员
protected IsysFunctionBLL funbll;
protected IsysKeyValueBLL keybll;
protected IsysMenusBLL menbll;
protected IsysOrganStructBLL orgbll;
protected IsysPermissListBLL perbll;
protected IsysRoleBLL rolbll;
protected IsysUserInfo_RoleBLL userrolbll;
protected IsysUserInfoBLL userbll;
protected IwfProcessBLL peocebll;
protected IwfRequestFormBLL reqbll;
protected IwfWorkBLL workbll;
protected IwfWorkBranchBLL brabll;
protected IwfWorkNodesBLL nodebll;
#endregion
----------------------------------------------
开始实现登录操作

首先 在 实体层 中 建一个类 LoginUserInfo

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public class LoginUserInfo
{
[DisplayName("账 号"),Required(ErrorMessage="账号不能为空")]
public string uLoginName { get; set; }
[DisplayName("密 码"), Required(ErrorMessage = "密码不能为空")]
public string uLoginPWD { get; set; }
[DisplayName("验证码"), Required(ErrorMessage = "验证码不能为空")]
public string Vcode { get; set; }
[DisplayName("3天免登录")]
public bool IsRemember { get; set; }
}

在 UI 层中 建一个控制器LoginController

首先 写一个Action 只做显示登录视图作用

/// <summary>
/// 显示登录视图的
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult Login()
{
// int i = 0; int j = 1; int e = j / i;
LoginUserInfo model = new LoginUserInfo() { uLoginName = "admin", uLoginPWD = "123456" };
//如何获取浏览器发送过来的cookie
if (Request.Cookies[Keys.IsRemember] != null)
{
model.IsRemember = true;
}

return View(model);
}

Action方法Login 是Get请求 显示强类型视图 类型为前面自定义的类 LoginUserInfo

视图中 用Ajax 异步表单 并使用ligerUI 插件美化

在视图加载中 使用 $.ligerDialog.open()弹窗

open方法中的 属性 target: $("#target1"),//将ajax异步表单建在此div中

<script type ="text/javascript">
$(function () {
$.ligerDialog.open({
target: $("#target1"),//将ajax异步表单建在此div中
width: 450, height: 280, title: "登录CRM管理系统",
buttons: [
{
text: ‘登录‘, onclick: function (item, dialog) {
$("#btlogin").click();
},
cls: ‘l-dialog-btn-highlight‘
},
{
text: ‘重置‘, onclick: function (item, dialog) {
//form0利用js重置表单的值 两种方法都可以
//document.getElementById("form0").reset();
$("#btreset").click();
}
}]
});
});
</script>

生成验证码

//验证码上鼠标移入事件
function sho() {
var b = document.getElementById("b");
b.innerHTML = "点击切换验证码";
}
//验证码上鼠标移出事件
function hid() {
var b = document.getElementById("b");
b.innerHTML = "";
}
---------------------------------
//点击刷新验证码
function reCode() {
$("#code").attr("src", "/admin/Vcode/Vcode?rid="+Math.random());
}
---------------------------------
<tr>
<th>@Html.DisplayNameFor(c=>c.Vcode)</th>
<td>
@Html.TextBoxFor(c => c.Vcode, new {style="width:100px" })
<img id="code" onclick="reCode()" onmouseover="sho();" onmouseout="hid();" src="/admin/Vcode/Vcode" style="width:65px;height:25px;cursor:pointer" />
<span id="b" style="color:#0094ff"></span>
@Html.ValidationMessageFor(c=>c.Vcode)
</td>
</tr>

生成验证码 单独建一个控制器
------------------------------
ajax 请求成功回调函数 OnSuccess="success" 用的很多 在此进行封装

function success(){

helper.CheckStatus(ajaxobj, function () {
if (ajaxobj.status == 1) {
$.ligerDialog.error(ajaxobj.msg, "错误提示");
return;
}
//此处会跳转到admin/Home/index
$.ligerDialog.success(ajaxobj.msg, "成功提示", function () {
window.location = ‘@Url.Action("index","Home")‘
});
//就算用户不点击延迟2秒钟 也能自己跳转
setTimeout(function () {
window.location = ‘@Url.Action("index","Home")‘
}, 2000);
});

}

在Scripts 文件夹中 见一个admin文件夹 再在此文件夹里 建一个 common.js 文件 封装在这文件里

var helper = {
//1.0封装所有的状态判断方法
CheckStatus:function(ajaxobj,callback){
if (ajaxobj.status == 1)//表示异常
{
$.ligerDialog.error(ajaxobj.msg);
} else if (ajaxobj.status == 2)//表示未登录
{
$.ligerDialog.error(ajaxobj.msg, "未登录提醒", function () {
window.top.location = "/admin/login/login";
});
//为点击确定 2秒自动跳转登录页面
setTimeout(function () { window.top.location = "/admin/login/login"; }, 2000);
} else if (ajaxobj.status == 3)//表示没有权限访问action
{
$.ligerDialog.warn(ajaxobj.msg, "无权限提醒");
} else
{
//触发正常逻辑处理
callback();
}
}

优化EF以及登录验证

标签:

原文地址:http://www.cnblogs.com/dianshen520/p/4349142.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!