0

Is it possible in asp.net to change the Session object so that when it is used to set/get , it does something else?

EX:

Session["variable"] I'll like it to do behind the covers Session[guid + variable] append to the key a GUID

Update:
Some of you asked way do I what to do this?
I want to do this to make the session unique for each tab in the browser. I'm forced to use the session. The guid will be a unique tab id. (see solution here)

Community
  • 1
  • 1
Daniel
  • 1,225
  • 2
  • 15
  • 31

4 Answers4

4

You could wrap Session with your own class that pre-pends a guid to the session key, and then use that class instead of Session. With that said, I'm not sure why you would want to.

Update

If you need the data to be unique for each browser tab, use ViewState instead. It's used to persist state between requests for a particular page/browser window/tab.

jrummell
  • 42,637
  • 17
  • 112
  • 171
  • 1
    +1 I agree. Don't see the reason to do this. It seems that OP thinks that Session and Cache are the same, hence the need for an extra unique identifier. – Icarus Mar 08 '12 at 19:44
  • @jrummell yes, i know about ViewState, but the legacy code that I'm changing forces me to use Session. Thanks anyway. – Daniel Mar 09 '12 at 08:02
  • @jrummell No I can not. If I could have i wouldn't have asked this question, in the first place. Thanks anyway. – Daniel Mar 09 '12 at 15:27
  • Ok, but you should know that you're hammering a nail with a screw driver. – jrummell Mar 09 '12 at 16:12
1

You can create your own session provider for ASP.NET and after this you gain full control over your session data.

Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
1

For Webforms, you can create a base System.Web.UI.Page class that all your pages inherit from that expose your methods. Like so:

public partial class _Default : BasePage {
    protected void Page_Load(object sender, EventArgs e) {
        Person p = new Person {
            FirstName = "MyFName",
            LastName = "MyLName"
        };
        SetSessionData<Person>("somevalue", p);
        var person = GetSessionData<Person>("somevalue");
    }
}

public class BasePage : System.Web.UI.Page {
    internal void SetSessionData<T>(string name, T value) {
        this.Session[string.Format("{0}_{1}", value.GetType().GUID, name)] = value;
    }
    internal T GetSessionData<T>(string name) {
        return (T)this.Session[string.Format("{0}_{1}", typeof(T).GUID, name)];
    }
}

public class Person {
    public Person() {
        ID = Guid.NewGuid();
    }
    public Guid ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

}

For MVC: Another suggestion would be to create a "Base" Page class that all your controllers inherit from that expose your methods. Like so:

public class BaseController : Controller   {
    internal void SetSessionData<T>(string name, T value) {            
        this.Session[string.Format("{0}_{1}",value.GetType().GUID,name)] = value;
    }
    internal T GetSessionData<T>(string name) {
        return (T)this.Session[string.Format("{0}_{1}", typeof(T).GUID, name)];
    }
}

Then, you can get or set items as so:

        Person p = new Person{
            FirstName = "MyFName",
            LastName  = "MyLName"
        };
        SetSessionData<Person>("somevalue", p);
        var person = GetSessionData<Person>("somevalue");
mkorman
  • 948
  • 2
  • 10
  • 27
anAgent
  • 2,550
  • 24
  • 34
0

It is not possible in the direct sense, without creating your own session provider. A possible work around could be to create two new methods. Possibly put them in the Global.asax

public String GetSession(String variable)
{
    return Session[guid+variable];
}

public void SetSession
{
        Session.Add(guid+variable, value);
}
Mark
  • 1,455
  • 3
  • 28
  • 51