Personally, I would think about separating the Roles collection, from the
Control, and cache the Roles first.
You could implement your own IPrincipal object.
When the user logs on correctly, you create, and cache the IPrincipal
object.
The User Control code looks like this:
{
if (null!= Session["MYKEY"])
{
MyPrincipal princ = Session["MYKEY"];
hyperlinkButton 1.Visible = princ.IsInRole( "ManageUser s");
hyperlinkButton 2.Visible = princ.IsInRole( "ManageDept s");
hyperlinkButton 3.Visible = princ.IsInRole( "ManageResource s");
}
else
{
// no items enabled ... show the login link
hyperlinkButton 4.Text = "You need to login";
hyperlinkButton 4.Visible = true;
}
}
IPrincipal implementation takes a little work, but its not rocket science.
You could then have a BusinessLogic object which authenticates a User like
this:
public void MyPrincipal BusinessLogic.U sers.Authentica teUser (string
userName, string password)
{
//check your database, and then populate your implementation of
IPrincipal with the roles.
ArrayList al = new ArrayList();
al.Add("Manager Users");
al.Add("ManageD epts");
// i am not adding ManageResources
return new MyPrincipal ( new MyIdentity("bri an") , al );
//you need to write a MyIdentity (implements IIdentity class also, which is
basically your name.
}
MyPrincipal looks something like this:
public class MyPrincipal : IPrincipal
{
private MyIdentity m_id;
private ArrayList m_roles;
public MyPrincipal (IIdentity id , ArrayList roles)
{
this.m_id = id;
this.m_roles = roles;
}
public bool IsInRole(string role)
{
//check the m_roles to see if it contains the role
return true; //stub return value
}
}
This way, you can reuse your MyPrincipal object, whereever you need it.
You can then try to cache the UserControl using traditional UserControl
methods.
But you haven't tied together your MyPrincipal , making a looser coupling
between the roles a user has, and the presentation of those roles.
...
IPrincipal and IIdentity are not that hard, it takes a little used to using.
"B" <brian_is_onlin e@yahoo
..co.uk> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hi,
I'm working on a site where the user must log in before they see the
home page.
When the user logs in, I retrieve their role from the database, store
it in a Session object and redirect to the homepage. The home page
contains a navigation menu (user control) which displays links
available to the user based on their role. The navigation menu/links
are generated from the database.
To minimize database calls, once the user logs in I'd like to
effectively cache the navigation menu user control for the duration of
that user's session. Does anyone have any idea how to do this?
I've had a look at setting Duration on @OutputCache but this causes
problems if a user logs out, and another user logs in with a different
role: the new user sees the old user's navigation menu.
Would Application level variables be more appropriate in this
situation? I have to say that I don't have much experience of caching
in ASP.NET so any help would be appreciated.
Thanks,
B