WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

MVC权限控制

Posted on 2016-12-03 05:54  WebEnh  阅读(261)  评论(0编辑  收藏  举报

基本方法是重写AuthorizeAttribute类的AuthorizeCore方法

protected override bool AuthorizeCore(HttpContextBase httpContext)

{

string currentRole = GetRole(httpContext.User.Identity.Name);

if (Roles.Contains(currentRole))

return true;

return base.AuthorizeCore(httpContext);

}

 

private string GetRole(string name)

{

Model1 db = new Model1();

student newstudent = db.Students.First(m => m.name == name);

return newstudent.name;

}

 

在登录的时候需要写入name信息

FormsAuthentication.SetAuthCookie(stu.name, false);

 

最后在需要控制的action前加上

[MyAuthAttribute2(Roles = "****")]