Wrox Programmer Forums
BOOK: ASP.NET Website Programming Problem-Design-Solution
This is the forum to discuss the Wrox book ASP.NET Website Programming: Problem - Design - Solution, Visual Basic .NET Edition by Marco Bellinaso, Kevin Hoffman; ISBN: 9780764543869
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET Website Programming Problem-Design-Solution section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 26th, 2004, 11:04 PM
Authorized User
Join Date: Nov 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Best way to persist Context.User across pages?

This has been discussed before. I am looking for response from guys who has tackled this problem smartly and efficiently.

The Context.User is set to SitePrincipal in login.aspx page after we read the user profile from DB.
  Context.User = newUser; <-- static SitePrincipal Object is saved

Now the user get redirected to some other page which inherits from PhilePage base class and its load() gets called. This base class has code:

    if (Context.User.Identity.IsAuthenticated)
                if (!(Context.User is PhilePrincipal))
                    PhilePrincipal newUser = new PhilePrincipal( Context.User.Identity.Name );
                    Context.User = newUser;

This second check always fails because Context.User is now of type GenericPrincipal even though I set it to SitePrincipal in login page.
This causes to reload the same profile again!!

Is there any way to prevent this reloading by saving Context.User?
What will be the best alternate way to save the user profile-a cookie OR session var or Caching?

Assume that my site will have 1000 users active all the time.


Old May 27th, 2004, 07:17 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts


To persist it across pages, you can use the Application, Session, or Cache objects, although I don't know what the best solution would be. I would probably recommend Caching, because it is meant for storing large volumes of data.

You could store the name of the user and recreate the object every time you need it, as you do above. You could also persist the object as a static method in a class or module that retrieves the principal object for the current user.

Lot's of ways to tackle the issue, depending on what you want to do.

Old May 29th, 2004, 07:30 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts

I'd use session state for user-specific security info. This gives more separation between users. One a single-server system it will stay in memory if you use in-process session state.

The cache is shared by all users. You can programatically keep them separate, but if there's a bug in your program you might have a security problem.

Old July 6th, 2005, 08:48 PM
Registered User
Join Date: Jul 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts

The following code will help avoid excessive trips to the database:

SitePrincipal cachedNewUser = (SitePrincipal)Session["cachedNewUser"];
if(!(cachedNewUser == null))
Context.User = cachedNewUser;
cachedNewUser = new SitePrincipal( Context.User.Identity.Name );
Session["cachedNewUser"] = cachedNewUser;
Context.User = (SitePrincipal)Session["cachedNewUser"];

Warning - You Cannot use absolute URL's for links or redirects, only
relative paths. This is because the User.Context will be set back to
GenericPricipal when Absolute addresses are interpreted by IIS.
I learned this the hard way by coding absolute paths in my Global.asax.cs file and using them in the SiteHeader control.
The original Phile code for SitePage does not suffer from this because the Context.User is being reset (albeit via a trip to the db) EVERY time a page is loaded.

I hope this helps!

Shawn Cohan
Old April 30th, 2007, 10:44 PM
Registered User
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

cohanshl's method help me

Old January 4th, 2008, 12:07 AM
Registered User
Join Date: Jan 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Dear Brian / Shawn Cohan / all members

I hv converted Phile in VS2005 its working fine in local computer,
but problem with remote/web server context.user does not presist identity with page wise.

Please explain what is the best method for overcome with context.user identity page wise.

some one advise me for caching, please explain a little bit how use caching for context.user or any other possible solutions.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Persist user selected check box information Parinisasa C# 0 May 28th, 2007 01:37 PM
(SitePrincipal)Context.User Philey BOOK: ASP.NET Website Programming Problem-Design-Solution 3 October 8th, 2004 05:38 PM
Context User davidroth BOOK: ASP.NET Website Programming Problem-Design-Solution 0 April 14th, 2004 12:49 AM
ASP Upload - does object persist across pages? James Diamond Classic ASP Components 4 January 27th, 2004 09:41 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.