欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET MVC使用ActionFilterAttribute实现权限限制的方法(附demo源码下载)

程序员文章站 2023-12-19 23:07:04
本文实例讲述了asp.net mvc使用actionfilterattribute实现权限限制的方法。分享给大家供大家参考,具体如下: actionfilterattri...

本文实例讲述了asp.net mvc使用actionfilterattribute实现权限限制的方法。分享给大家供大家参考,具体如下:

actionfilterattribute是action过滤类,该属于会在执行一个action之前先执行.而actionfilterattribute是 mvc的一个专门处理action过滤的类.基于这个原理 我们做一个权限限制

例如:如何访问 homecontroller  里的test  action

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;
using mvctest.models;
namespace mvctest.controllers
{
  public class homecontroller : controller
  {
    //
    // get: /home/
    public actionresult index()
    {
      return view();
    }
    [authorizefilter]
    public actionresult test()
    {
      return content("有权访问");
    }
  }
}

建立authorizefilterattribute.cs   内容如下

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;
namespace mvctest.models
{
  /// <summary>
  /// 权限拦截
  /// </summary>
  [attributeusage(attributetargets.class | attributetargets.method, allowmultiple = false)]
  public class authorizefilterattribute : actionfilterattribute
  {
    filtercontextinfo fcinfo;
    // onactionexecuted 在执行操作方法后由 asp.net mvc 框架调用。
    // onactionexecuting 在执行操作方法之前由 asp.net mvc 框架调用。
    // onresultexecuted 在执行操作结果后由 asp.net mvc 框架调用。
    // onresultexecuting 在执行操作结果之前由 asp.net mvc 框架调用。
    /// <summary>
    /// 在执行操作方法之前由 asp.net mvc 框架调用。
    /// </summary>
    /// <param name="filtercontext"></param>
    public override void onactionexecuting(actionexecutingcontext filtercontext)
    {
      fcinfo = new filtercontextinfo(filtercontext);
      //fcinfo.actionname;//获取域名
      //fcinfo.controllername;获取 controllername 名称
      bool isstate = true;
      //islogin = false;
      if (isstate)//如果满足
      {
        //逻辑代码
        // filtercontext.result = new httpunauthorizedresult();//直接url输入的页面地址跳转到登陆页 
        // filtercontext.result = new redirectresult("http://www.baidu.com");//也可以跳到别的站点
        //filtercontext.result = new redirecttorouteresult(new system.web.routing.routevaluedictionary(new { controller = "product", action = "default" }));
      }
      else
      {
        filtercontext.result = new contentresult { content = @"抱歉,你不具有当前操作的权限!" };// 直接返回 return content("抱歉,你不具有当前操作的权限!")
      }
    }
    /// <summary>
    /// 在执行操作方法后由 asp.net mvc 框架调用。
    /// </summary>
    /// <param name="filtercontext"></param>
    public override void onactionexecuted(actionexecutedcontext filtercontext)
    {
      base.onactionexecuted(filtercontext);
    }
    /// <summary>
    /// onresultexecuted 在执行操作结果后由 asp.net mvc 框架调用。
    /// </summary>
    /// <param name="filtercontext"></param>
    public override void onresultexecuted(resultexecutedcontext filtercontext)
    {
      base.onresultexecuted(filtercontext);
    }
    /// <summary>
    /// onresultexecuting 在执行操作结果之前由 asp.net mvc 框架调用。
    /// </summary>
    /// <param name="filtercontext"></param>
    public override void onresultexecuting(resultexecutingcontext filtercontext)
    {
      base.onresultexecuting(filtercontext);
    }
  }
  public class filtercontextinfo
  {
    public filtercontextinfo(actionexecutingcontext filtercontext)
    {
      #region 获取链接中的字符
      // 获取域名
      domainname = filtercontext.httpcontext.request.url.authority;
      //获取模块名称
      // module = filtercontext.httpcontext.request.url.segments[1].replace('/', ' ').trim();
      //获取 controllername 名称
      controllername = filtercontext.routedata.values["controller"].tostring();
      //获取action 名称
      actionname = filtercontext.routedata.values["action"].tostring();
      #endregion
    }
    /// <summary>
    /// 获取域名
    /// </summary>
    public string domainname { get; set; }
    /// <summary>
    /// 获取模块名称
    /// </summary>
    public string module { get; set; }
    /// <summary>
    /// 获取 controllername 名称
    /// </summary>
    public string controllername { get; set; }
    /// <summary>
    /// 获取action 名称
    /// </summary>
    public string actionname { get; set; }
  }
}

完整实例代码点击此处本站下载

希望本文所述对大家asp.net程序设计有所帮助。

上一篇:

下一篇: