Any suggestion for ASP.NET caching mechanism on this?

B

B

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
 
G

Guest

B,
Suggestions:
1) Consider using Forms Authentication against your database, since it has
much of what you describe (roles, for example) already built-in.
2) To cache user-specific permissions relating to the display of a
navigation menu, you should consider setting up the menu with a public Role
property which determines what it displays. You can then use the
Page.User.IsInRole("admin") method of the authenticated user and you won't
need to cache anything.

Setting OutputCache on the control won't help you in this situation since
the control needs to be displaying specific to the authenticated user.
 
S

sloan

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"];

hyperlinkButton1.Visible = princ.IsInRole("ManageUsers");
hyperlinkButton2.Visible = princ.IsInRole("ManageDepts");
hyperlinkButton3.Visible = princ.IsInRole("ManageResources");

}
else
{
// no items enabled ... show the login link
hyperlinkButton4.Text = "You need to login";
hyperlinkButton4.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.Users.AuthenticateUser (string
userName, string password)

{

//check your database, and then populate your implementation of
IPrincipal with the roles.
ArrayList al = new ArrayList();
al.Add("ManagerUsers");
al.Add("ManageDepts");
// i am not adding ManageResources

return new MyPrincipal ( new MyIdentity("brian") , 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_online@yahoo

..co.uk> wrote in message
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top