ASP.NET MVCはActionFilterAttributeを利用して権限等を行う

13639 ワード

ActionFilterAttributeは、アクションを実行する前に実行するアクションフィルタクラスである.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; }

}

}

DEMOダウンロード