如何设置基于数据库表的操作方法授权的访问? [英] How do i set authorized access to action methods based on DB tables?
本文介绍了如何设置基于数据库表的操作方法授权的访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 mst_roles
表与followingstructure分贝
i have a mst_roles
table in db with followingstructure
id RoleName
1 Admin
2 Manager
3 Operator
在 mst_users
表是这样的。
id username password RoleId
1 bob 123 2
2 rick 777 3
在我的MVC我有一个控制器订单
有两个动作方法
in my MVC i have a controller Orders
with two action methods
public ActionResult TakeOrder()
{
}
public ActionResult StopAllTransactions()
{
}
我怎么只让角色的管理访问 StopAllTransaction()
和操作有访问 TakeOrder()
?
推荐答案
操作方法:
[AuthorizeDBRoleAttribute(Roles = "Role1,Role2")]
public ActionResult Welcome()
{
return View();
}
自定义类:
public class AuthorizeDBRoleAttribute : AuthorizeAttribute
{
public string Roles { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContextBase)
{
//Bind User Roles from Database here
string userRoles = "Role1,Role2,Role3";
if (userRoles.IndexOf(Roles) > -1)
return true;
else
return false;
}
}
这篇关于如何设置基于数据库表的操作方法授权的访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文