Let's go

第一节:登录流程

一丶登录流程

1.线性槽

 public class DbContextFactory
    {
        public static DbContext GetDbContext()
        {
            DbContext dbContext = CallContext.GetData(typeof(DbContextFactory).Name) as DbContext;
            if (dbContext == null)
            {
                dbContext = new DBTestEntities();
                //写入线程槽
                CallContext.SetData(typeof(DbContextFactory).Name, dbContext);
                //DbContext dbContext2 = CallContext.GetData(typeof(DbContextFactory).Name) as DbContext;
            }
            return dbContext;
        }
    }
View Code

2.登录页面

js:

   <script type="text/javascript">
        $(function () {
            //账号文本框进行变色(按下的过程,被按下,键被松开)
            $("#j_number").keydown(function () {
                $(this).css("background-color", "yellow");
            });
            $("#j_number").keypress(function () {
                $(this).css("background-color", "blue");
            });

            $("#j_number").keyup(function () {
                $(this).css("background-color", "pink");
            });
            //回车实现登录
            $("body").keydown(function (event) {
                if (event.keyCode == "13") {//keyCode=13是回车键
                    $("#j_btn").click();
                }
            });
            //点击登录
            $("#j_btn").click(function () {
                var number = $("#j_number").val();
                var password = $("#j_password").val();
                if (number == "") {
                    alert("请输入账号");
                    $("#j_number").focus();
                    return;
                }
                if (password == "") {
                    alert("请输入密码");
                    $("#j_password").focus();
                    return;
                }
                $.post("/Login/CheckLogin", {"number":number,"password":password}, function (data) {
                    if (data == "ok") {
                        window.location.href="/Main/Index";
                    } else {
                        alert("账号或密码错误,请重新输入");
                    }
                });
            });
        });
    </script>

页面:

     <table>
            <tr><td>账号:</td><td><input type="text" id="j_number"/></td></tr>
            <tr><td>密码:</td><td><input type="password" id="j_password" /></td></tr>
            <tr><td><input type="button" value="登录"id="j_btn" /></td></tr>
        </table>

业务:

 public ActionResult CheckLogin(string number, string password)
        {
            //查询数据库是否存在此账号密码
            var userInfo = db.Set<UserInfo>().Where(a => a.Number == number && a.Password == password).FirstOrDefault();
            if (userInfo != null)
            {
                Session["userInfo"] = userInfo;
                return Content("ok");
            }
            else
            {
                return Content("error");
            }
        }
View Code

Filter:

/// <summary>
    /// 校验登录过滤器
    /// </summary>
    public class CheckIsLogin : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (HttpContext.Current.Session["userInfo"] == null)
            {
                filterContext.Result = new RedirectResult("/Login/Login");             
            }
        }
    }
View Code
posted @ 2018-07-06 11:01  chenze  阅读(231)  评论(0编辑  收藏  举报
有事您Q我