0

I am using Session wrapper like described in answer here: How to access session variables from any class in ASP.NET?

But I don't know how to do Session.Clear() and Session.Abandon() using this principle.

My code:

public class AppSession
{
    // private constructor
    private AppSession()
    {
        CurUser = new UserHolder();
    }

    // Gets the current session.
    public static AppSession Current
    {
        get
        {
            AppSession session =
                (AppSession) HttpContext.Current.Session["__AppSession__"];
            if (session == null)
            {
                session = new AppSession();
                HttpContext.Current.Session["__AppSession__"] = session;
            }
            return session;
        }
    }

    // **** add your session properties here, e.g like this:

    // Current app user
    public UserHolder CurUser { get; set; }
}
Community
  • 1
  • 1
goran85
  • 503
  • 5
  • 19
  • Do you want to abandon the actual session or just your AppSession? Becuase you can access the current session via HttpContext.Current.Session, and you can simply call Clear() or Abandon() on it. – Oguz Ozgul Dec 01 '15 at 14:23

1 Answers1

0

You could easily do:

public class AppSession
{
   public void Clear()
   {
      //Remove items from the AppSession class
      //Update AppSession instance in HTTP Context session
      HttpContext.Current.Session["__AppSession__"] = this;
   }
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • Could you explain how calling "this" will clear the actual object? To mee it would seem that it just returns the instance with the get method in the wrapper class? – Agneum Aug 03 '18 at 12:50