嘿嘿!今天看见同事在搞MVC3的权限验证,以前不是很懂MVC3,自己研究了一下。希望对跟我一样的菜鸟童鞋有点点点帮助吧!
public
class
RoleFilter : FilterAttribute, IAuthorizationFilter
{
#region IAuthorizationFilter 成员
/// <summary>
/// 产生随机数判断是否具有权限访问
/// </summary>
/// <param name="filterContext"></param>
public
void
OnAuthorization(AuthorizationContext filterContext)
{
Random random =
new
Random();
int
i = random.Next(0, 10);
if
(i > 3)
{
filterContext.Result =
new
RedirectResult(ConfigurationManager.AppSettings[
"Url"
] +
"/Error/Index/"
+ i);
}
}
#endregion
}
下面是在Action上面加上 就可以验证了:
[RoleFilter()]
public
ActionResult Del(
int
id)
{
AlbumModels albumModels = GetModels();
albumModels.DelAlbum(id);
return
View();
}