public class RoleFilter : FilterAttribute, IAuthorizationFilter
|
02 |
{
|
03 |
04 |
#region IAuthorizationFilter 成员
|
05 |
06 |
/// <summary>
|
07 |
/// 产生随机数判断是否具有权限访问
|
08 |
/// </summary>
|
09 |
/// <param name="filterContext"></param>
|
10 |
public void OnAuthorization(AuthorizationContext filterContext)
|
11 |
{
|
12 |
Random random = new Random();
|
13 |
int i = random.Next(0, 10);
|
14 |
if (i > 3)
|
15 |
{
|
16 |
filterContext.Result = new RedirectResult(ConfigurationManager.AppSettings["Url"] + "/Error/Index/" + i);
|
17 |
}
|
17 |
|
18 |
}
|
19 |
20 |
#endregion
|
21 |
22 |
}
|
代码比较简单产生个随机数,如果大于3跳转到错误页面~~这个地方可以从数据库中读取权限配置~~例如用户的服务端权限验证
例如在上边代码块用httpContext.User.Identity.Name来获取用户唯一信息,当然,要在用户登录的时候设置此信息,如下:
1 |
|
1 |
[RoleFilter()] |
2 |
public ActionResult Del(int id)
|
3 |
{
|
6 |
return View();
|
7 |
}
|
这样即可完成权限!