Fork me on GitHub
完成注销 登录限制过滤 添加用户

完成注销 登录限制过滤 添加用户

前面视频 文章地址

 
这节课 我们要实现 一个登录的限制

如果用户没有登录 就访问我们的管理页面 那么 直接跳转到登录 当然 可以可以给一个中间的页面 对用户进行友好的提示 

我们首先找到 管理页的action
 
复制代码
        public ActionResult Index()
        {
            return View();
        }
复制代码

我们编写一个过滤器 要继承和实现一个接口

复制代码
    public class CheckLoginFilter : FilterAttribute, IActionFilter
    {

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (HttpContext.Current.Session["user"] == null)
            {
                filterContext.HttpContext.Response.Write("-1");
            }
        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (HttpContext.Current.Session["user"] == null)
            {
               //filterContext.HttpContext.Response.Write("-1");
                try
                {
                    filterContext.Result = new RedirectResult("/Account/Login");
                }
                catch (Exception)
                {
                    filterContext.Result = new RedirectResult("/Common/Error");
                }
            }
        }
    }
复制代码

然后 为管理员打上标记

复制代码
        [CheckLoginFilter()]
        public ActionResult Index()
        {
            return View();
        }
复制代码

用户添加页面的设计

复制代码
<div id="tb" style="padding:5px;height:auto">

                <div style="margin-bottom:5px">
                        <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true"></a>
                        <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true"></a>
                        <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true"></a>
                        <a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true"></a>
                        <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true"></a>
                </div>

                <div>
                        用户名: <input type="text" id="name" style="width:80px">
                        密 码: <input type="text" id="pwd" style="width:80px">
                        技 术: 
                        <select id="tec" class="easyui-combobox" panelHeight="auto" style="width:100px">
                                <option value="java">Java</option>
                                <option value="c">C</option>
                                <option value="basic">Basic</option>
                                <option value="perl">Perl</option>
                                <option value="python">Python</option>
                        </select>
                        <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="AddUser();">添加</a>
                </div>

        </div>
复制代码

添加提交事件

复制代码
function AddUser() {
    //$.messager.alert('Warning', '你真的添加吗!');
    var name = $('#name').val();
    var pwd = $('#pwd').val();
    var tec = $('#tec').val();

    if (name == '' || pwd == '') {
        $.messager.alert('Warning', '用户名或者密码为空!');
    }
    else {
        $.post("/Account/AddUser", { name: name, name: pwd },
           function (data) {
               //alert("Data Loaded: " + data);
               if (data == '0') {
                   $.messager.alert('Warning', '添加失败!');
               }
               else {
                   $.messager.alert('Warning', '添加成功!');
               }
           });
    }
}
复制代码

 

高清录屏下载地址

18-19节

http://pan.baidu.com/share/link?shareid=1882807484&uk=1731339785

20节

http://pan.baidu.com/share/link?shareid=473445811&uk=36858893

 

21-23节

http://pan.baidu.com/share/link?shareid=1857442884&uk=1731339785

需要源码的:http://www.bamn.cn/thread-64-1-1.html?usersystem.rar

posted on 2013-08-15 12:33  HackerVirus  阅读(206)  评论(0编辑  收藏  举报