Losing Session after inheriting custom System.Web.UI.Page class

Please help.

I've created a class class "ExtranetPage" that I want to inherit in each of my pages throughout my ASP.NET application. Why? Because I want certain information to be easily accessible throughout the application, like ActiveUser, Permissions, etcetera.

It works except that I keep losing my session after a redirect (maybe even a postback -- i haven't gotten that far).

How can I persist my Session? See code ...

// here the code behind of webform ...
public partial class SomeWebProject_Default : ExtranetPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
            this.label1.Text = ActiveSession.User.FirstName + ActiveSession.User.LastName;
        
    }
}
 
 
// here's the class i'm inheriting ...
public class ExtranetPage : System.Web.UI.Page
{
 
    private ExtranetSession _extranetSession;
 
    public ExtranetSession ActiveSession
    {
        get 
        {
            if (this._extranetSession == null)
                this._extranetSession = new ExtranetSession(Session.SessionID);
 
            return this._extranetSession; 
        }
    }
}
 
 
 
// it uses this ...
public sealed class ExtranetSession
{
    private string _sessionID;
    private PSCGlobalDataContext _db;
    private GlobalUser _user;
 
    public ExtranetSession(string SessionID)
    {
        this._sessionID = SessionID;
    }
 
    public PSCGlobalDataContext Database
    {
        get
        {
            if (this._db == null)
                this._db = new PSCGlobalDataContext();
 
            return this._db;
        }
    }
 
    public string this[string key]
    {
        get
        {
            string returnVal = "";
 
            if (Database.CustomSessionStateDatas.Any(x=>x.KeyName == key && x.SessionID == this._sessionID))
                returnVal = Database.CustomSessionStateDatas.Single(x=>x.KeyName == key && x.SessionID == this._sessionID).KeyValue;
 
            return returnVal;
 
        }
        set
        {
 
            if (Database.CustomSessionStateDatas.Any(x => x.KeyName == key && x.SessionID == this._sessionID))
            { 
                // update
                CustomSessionStateData data = Database.CustomSessionStateDatas.Single(x => x.KeyName == key && x.SessionID == this._sessionID);
 
                data.KeyValue = value;
 
                Database.SubmitChanges();
            }
            else
            {
                
                // insert
                CustomSessionStateData data = new CustomSessionStateData();
 
                data.SessionID = this._sessionID;
                data.KeyName = key;
                data.KeyValue = value;
                data.DateTimeStamp = DateTime.Now;
 
                Database.CustomSessionStateDatas.InsertOnSubmit(data);
                Database.SubmitChanges();
            }
 
        }
    }
 
    public GlobalUser User
    {
        get
        {
 
            if (this["ActiveUserID"].Equals(""))
                return null;
 
            string test = this["ActiveUserID"];
 
            test = test;
 
            if (this._user == null)
                this._user = Database.GlobalUsers.Single(x => x.ID == long.Parse(this["ActiveUserID"]));
 
            return this._user;
 
        }
    }
 
}

Open in new window

twelvetwentyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi twelvetwenty;

I am not expert in this area but can you please give so info:

1. Is this application part of a Web Farm?
2. If Web Farm what does the <sessionState> element of the web.config file look like?
3. When you redirect and lose session state info is the target of the redirect in the same application on the same server?

Fernando
0
twelvetwentyAuthor Commented:
1. Application is on single server
2. N/A
3. Yes, same application, same server

I think the problem has something to do with the fact that I may be instantiating a new Session object somehow when creating my own ExtranetPage object.

How do I persist the ExtranetPage object throughout my application?
0
twelvetwentyAuthor Commented:
I was missing something in web.config:

<sessionState mode="InProc" cookieless="true" timeout="20" />

Thanks for the help.
0
Fernando SotoRetiredCommented:
I figured it was a configuration issue. Glad you got it going. ;=)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
twelvetwentyAuthor Commented:
I appreciate your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.