我一直在学习如何从一些教程中实现CustomRoleProvider,并设法实现以下两种主要方法
public override string[] GetRolesForUser(string userName)
{
string connectionString =
ConfigurationManager.ConnectionStrings["myDb"].ConnectionString;
DataContext context = new DataContext(connectionString);
Table<UserObj> usersTable = context.GetTable<UserObj>();
UserObj userObj = usersTable.SingleOrDefault(u => u.UserName == userName);
string roleId = userObj.UserRoleID;
if (roleId != null)
return roleId.Select(c => c.ToString()).ToArray();
else
return new string[] { };
}
public override bool IsUserInRole(string userName, string roleName)
{
string connectionString =
ConfigurationManager.ConnectionStrings["myDb"].ConnectionString;
DataContext context = new DataContext(connectionString);
Table<UserObj> usersTable = context.GetTable<UserObj>();
UserObj userObj = usersTable.SingleOrDefault(u => u.UserName == userName);
if (userObj != null)
{
string roleId = userObj.UserRoleID;
if (roleId.Equals(roleName))
return true;
}
return false;
}
然后我在控制器的索引方法上添加了[Authorize(Roles =“admin”)],我只想让admin获得访问权限。当我尝试访问该页面时,它似乎执行了限制,例如,如果我输入了url:
http://localhost:60353/module
..它将我重定向到
http://localhost:60353/Account/LogOn?ReturnUrl=%2fmodule
但是,似乎没有检查这个角色。
我在这里做错了什么?
答案 0 :(得分:0)
我也面临同样的问题,但我可以调用CustomProvider方法:
IsUserInRoles()
显式,但它似乎是正确的,因为可访问性没有改变....它总是重定向到登录屏幕.......