public class RoleFilter : FilterAttribute, IAuthorizationFilter |
04 |
#region IAuthorizationFilter 成员 |
09 |
/// <param name="filterContext"></param> |
10 |
public void OnAuthorization(AuthorizationContext filterContext) |
12 |
Random random = new Random(); |
13 |
int i = random.Next(0, 10); |
16 |
filterContext.Result = new RedirectResult(ConfigurationManager.AppSettings[ "Url" ] + "/Error/Index/" + i); |
17 |
//例如:var Name=httpContext.User.Identity.Name;
// if(Name!="Admin") 这里可做到数据库检查做权限验证
//do something for failed,you can go to registe page
|
代码比较简单产生个随机数,如果大于3跳转到错误页面~~这个地方可以从数据库中读取权限配置~~例如用户的服务端权限验证
例如在上边代码块用httpContext.User.Identity.Name来获取用户唯一信息,当然,要在用户登录的时候设置此信息,如下:
1 |
System.Web.Security.FormsAuthentication.SetAuthCookie("Admin", true); |
2 |
public ActionResult Del( int id) |
这样即可完成权限!