【发布时间】:2012-01-16 14:44:55
【问题描述】:
我正致力于在一个 mvc3 应用程序上实现用户权限管理。
我已经在数据库上定义了我的操作方法,其中 ControllerName、ActionName 和参数包括 ParameterName 和 ParameterType 等。
我实现了一个继承自 Authorize 属性的自定义属性。
我要做的是在我在数据库上定义的内置操作中找到执行的操作,并计算用户是否具有指定操作的权限。
代码是这样的;
[HttpPost]
[MyAuthorize]
public ActionResult Edit(VendorPageItem entity)
{
//...
}
public class MyAuthorize: System.Web.Mvc.AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext == null)
throw new ArgumentNullException("httpContext");
string controller = httpContext.Request.RequestContext.RouteData.Values["controller"].ToString();
string action = httpContext.Request.RequestContext.RouteData.Values["action"].ToString();
int userId = SessionState.Current.LoginParameter.VendorUserID;
List<string> parameterTypes = new List<string>();
//TODO: Find out action method parameter types.
return IoCWorker.Resolve<IUserRightService>().HasUserRightToAction(userId, controller, action, parameterTypes);
}
}
我的问题是在我的自定义属性中查找方法参数类型。
谢谢。
编辑:忘了提到那是后期动作。 [HttpPost] 已添加。
【问题讨论】:
-
我用这种方式玩过
AuthorizeAttribute,但发现在控制器方法中检查权限更容易。
标签: c# asp.net-mvc-3 custom-attributes