To make our role-based controls work with NHibernate, we need to implement the System.Web.Security.RoleProvider
abstract class. This abstract class is specifically designed to allow us to implement the ASP.NET role model using our own logic.
Just like the membership provider, the role provider has about 15 methods and properties that we can override, if we need to. However, just like the other provider, we really only need to focus on one method to implement the base class—GetRolesForUser()
.
GetRol
esForUser()
is passed one variable, the UserName as a string, and returns an array of strings (string[]
) containing the names of the roles to which the user belongs.
The following code snippet shows a simple NHibernate implementation of GetRolesForUser()
:
public override string[] GetRolesForUser(string UserName) { ArrayList roleList = new ArrayList(); IList<Role> roles = RoleDataControl.Instance.GetRolesByUserName(UserName); foreach (Role role in roles) { roleList...