zoukankan      html  css  js  c++  java
  • ASP.NET MVC 自己实现登陆验证过滤器

    1、首先添加一个过滤器类,并实现接口中对应的方法

    public class YLFAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
    {
      public void OnAuthorization(AuthorizationContext filterContext)
      {
        //获取控制器名称     
    string control= filterContext.Controller.ToString();
        //获取Action名称     
    string action = filterContext.ActionDescriptor.ActionName;
        //判断登陆标记,如果不成功了,页面跳转     
    if (filterContext.HttpContext.Session["UserName"] == null)       filterContext.Result = new RedirectResult("/home/index");   } }

    2、将你的过滤属性添加到对应的Controller类的上方,则对此控制器内所有Action进行登陆验证,如果只添加到某个Action上,可以只对某个Action进行登陆验证。

  • 相关阅读:
    Guava的学习2
    Guava的学习1
    数据结构
    二叉搜索树的第k个结点
    滑动窗口的最大值
    僵尸进程和孤儿进程
    fork和vfork,exec
    扑克牌顺子
    字符流中第一个不重复的字符
    表示数值的字符串
  • 原文地址:https://www.cnblogs.com/feihusurfer/p/3989785.html
Copyright © 2011-2022 走看看