I've been learning how to implement CustomRoleProvider from a few tutorials and managed to implement 2 main methods as below
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;
}
Then I added [Authorize(Roles = "admin")] on the index method of a controller which I want only admin to get access. When I tried to access the page, it seems to perform a restriction ok, for example, if I entered url:
http://localhost:60353/module
..it redirected me to
http://localhost:60353/Account/LogOn?ReturnUrl=%2fmodule
However, the role didn't seem to be checked.
What have I done wrong here?