在WEB项目中建立 类:

     public class LoginFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //HttpContext.Current.Response.Write("OnActionExecuting:正要准备执行Action的时候但还未执行时执行<br />");
            if (HttpContext.Current.Session"admin" == null)
            {
                //HttpContext.Current.Response.Write("js代码");
                //HttpContext.Current.Response.End();
                //return;

var context = new ContentResult();
                context.Content = "<script>alert('请重新登录!');location.href='/Login/Index'</script>";

filterContext.Result = context;

 
            }
        }
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //HttpContext.Current.Response.Write("OnActionExecuted:Action执行时但还未返回结果时执行<br />");
        }
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            // HttpContext.Current.Response.Write("OnResultExecuting:OnResultExecuting也和OnActionExecuted一样,但前者是在后者执行完后才执行<br />");
        }
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            // HttpContext.Current.Response.Write("OnResultExecuted:是Action执行完后将要返回ActionResult的时候执行<br />");
        }
    }

使用方法,直接在控制里方法外面写,如:

[LoginFilter]
        public ActionResult Add()
        {
            Model.Userinfo loginuser = base.GetLoginUser();
            Model.Product pro = new Model.Product() { userid = loginuser.id, username = loginuser.username };
            return View(pro);
        }

asp.net mvc中的用户登录验证过滤器的更多相关文章

  1. ASP.NET MVC下判断用户登录和授权的方法

    日常开发的绝大多数系统中,都涉及到管理用户的登录和授权问题.登录功能(Authentication),针对于所有用户都开放:而授权(Authorization),则对于某种用户角色才开放. 在asp. ...

  2. ASP.NET MVC下判断用户登录和授权状态方法

    在我们日常开发的绝大多数系统中,都涉及到管理用户的登录和授权问题.登录功能(Authentication),针对于所有用户都开放:而授权(Authorization),则对于某种用户角色才开放. 在a ...

  3. 在ASP.NET MVC中对手机号码的验证

    在ASP.NET MVC中,可以使用RegularExpression特性来验证手机号码. public class Customer { [Required(ErrorMessage = " ...

  4. ASP.NET MVC 中使用用户控件——转

    讲讲怎么在 ASP.NET MVC2中使用用户控件.首先我们新建一个用户控件,   我们命名为SelectGroup.ascx,代码如下 <%@ Control Language="C ...

  5. ASP.NET MVC项目演练:用户登录

    ASP.NET MVC 基础入门 http://www.cnblogs.com/liunlls/p/aspnetmvc_gettingstarted.html 设置默认启动页面 public clas ...

  6. ASP.NET MVC实现单用户登录

    现在许多网站都要求登录后才能进行进一步的操作,当不允许多用户同时登录一个帐号时,就需要一种机制,当再登录一个相同的帐号时,前面登录的人被挤下线,或者禁止后面的人登录.这里实现的是前一种功能. 网上有许 ...

  7. django 从零开始 8 用户登录验证 待测

    看文档 djang 自带一个用户登录验证的方法,不过有些看着懵逼,去网上找了一圈,发现很多都是照抄文档说明的,几乎没说啥原理 特别是 from django.contrib.auth import a ...

  8. C# MVC 用户登录状态判断 【C#】list 去重(转载) js 日期格式转换(转载) C#日期转换(转载) Nullable<System.DateTime>日期格式转换 (转载) Asp.Net MVC中Action跳转(转载)

    C# MVC 用户登录状态判断   来源:https://www.cnblogs.com/cherryzhou/p/4978342.html 在Filters文件夹下添加一个类Authenticati ...

  9. .NET跨平台之旅:ASP.NET Core从传统ASP.NET的Cookie中读取用户登录信息

    在解决了asp.net core中访问memcached缓存的问题后,我们开始大踏步地向.net core进军——将更多站点向asp.net core迁移,在迁移涉及获取用户登录信息的站点时,我们遇到 ...

随机推荐

  1. openerp 7.0邮件多用户发送失败问题 解决方法

    方法一(推荐): 修改代码/usr/lib/pymodules/python2.7/openerp/addons/base/ir/ir_mail_server.py #425 line: #mail_ ...

  2. jQuery get selected text from SELECT (or DROPDOWN) list box

    Most of the time in JavaScript we want to do following things with Select (or dropdown) list box. – ...

  3. Ubuntu与Windows7双系统下, 系统时间不一致的问题

    Ubuntu使用的UTC时间, 而Windows使用的是Local Time, 就导致每次切换系统后, Windows时间都会正好晚8个小时. 有两种解决办法, 一个是修改Ubuntu, 另一个是修改 ...

  4. 【OT1.0 + TP3.2】开启trace调试、输出调试信息、开启自定义菜单

    1.开启trace调试 A- 后台系统设置 show-page-trace = 1 B-config.php文件.配置 show-page-trace = true 2.输出调试信息 很奇怪,OT竟然 ...

  5. Heroku免费版限制

    SLEEPS AFTER 30 MINS OF INACTIVITY   30分钟无人访问就休眠 Verified accounts come with a monthly pool of 1000 ...

  6. QQ 互联认证 回调地址提示说要http :// 但是事实不用

    真奇怪 腾讯最近人手不够吧 这样的错误也会犯错....

  7. ios中推送

    http://www.cnblogs.com/cdts_change/p/3240893.html 推荐:http://blog.csdn.net/zhuqilin0/article/details/ ...

  8. easyui的datagrid分页写法小结

    easyui的datagrid分页死活不起作用...沙雕了...不说了上代码 //关闭tab1打开tab2 查询Detail function refundDetail(){ $('#tt').tab ...

  9. 搭建简单的网络部分(OC)框架

    准备工作 1.文件目录结构示图(按照MVC分层) 文件目录结构图/自定义Cell Controller: CYXOneViewController Model: CYXMenu View: CYXCe ...

  10. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)

    Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...