Doo*_*ght 3 asp.net security attributes asp.net-mvc-3
在我的数据库中,系统用户有一个他/她可以访问的模块列表.
我希望能够添加一个authorize属性来检查是否是这种情况.
例如 [authorise(UserID, ControllerName)]
对于某些代码,确保指定了UserID的用户在其列表中具有控制器名称.
目前,您可以通过使用URL简单地绕过标签不可见的事实.(我的代码已经检查用户是否已指定访问权限并隐藏/显示标签)
public class MyAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized)
{
return false;
}
string currentUser = httpContext.User.Identity.Name;
string currentController = httpContext.Request.RequestContext.RouteData.GetRequiredString("controller");
// TODO: go hit your database and see if currentUser can access
// currentController and return true/false from here
...
}
}
Run Code Online (Sandbox Code Playgroud)
然后装饰你的控制器或动作:
[MyAuthorize]
public class FooController: Controller
{
...
}
Run Code Online (Sandbox Code Playgroud)
这就是说我怀疑你可能在数据库设计中走错了方法,存储了哪个用户有权访问哪个控制器动作的列表.可能你应该使用角色.让数据库了解控制器只是错了.
所以:
[Authorize(Roles = "Foo,Bar")]
public class FooController: Controller
{
...
}
Run Code Online (Sandbox Code Playgroud)
只有具有用户Foo或Bar角色可以访问FooController.