[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

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

0
twelvetwenty
Asked:
twelvetwenty
  • 3
  • 2
1 Solution
 
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
 
twelvetwentyAuthor Commented:
I appreciate your help.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now