terbubbs
terbubbs

Reputation: 1512

Sitefinity - Remove Session after Logout

I am trying to clear a HttpContext.Current.Session after User logs out of a Sitefinity page.

I saw in this link that you can check the Request.Url but I'm not exactly sure what the implementation is.

This is my current attempt:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    if (((System.Web.HttpApplication)(sender)).Request.Url.ToString() == HttpContext.Current.Server.MapPath("~/Sitefinity/Login/DoLogout"))
    {
        if (HttpContext.Current.Session["Cart"] != null) HttpContext.Current.Session.Remove("Cart");
        HttpContext.Current.Session["Cart"] = new List<IQuoteResult>();
    }
}

Please let me know if you have any tips or suggestions, or if I'm completely wrong with my logic.

Thanks in advance.

UPDATE:

protected void Application_PostAcquireRequestState(object sender, EventArgs e)
    {
        if (((HttpApplication)(sender)).Request.Url.ToString().Contains("sign_out=true"))
        {
            if (HttpContext.Current.Session["Cart"] != null)
            {
                HttpContext.Current.Session.Remove("Cart");
                HttpContext.Current.Session["Cart"] = new List<IQuoteResult>();
            }
        }
    }

This is my next attempt at completing the same task but I keep receiving a NullReferenceException...

Note: I've also tried this method in the Application_AcquireRequestState method.

Here is the stack:

[NullReferenceException: Object reference not set to an instance of an object.]
SitefinityWebApp.Global1.Application_PostAcquireRequestState(Object sender, EventArgs e) +137
 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +91
 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +164

Upvotes: 1

Views: 815

Answers (2)

terbubbs
terbubbs

Reputation: 1512

This ended up being my solution:

public bool IsUserLoggingOut { get; set; }

protected void Application_PostAcquireRequestState(object sender, EventArgs e)
{
    if (((HttpApplication)(sender)).Request.Url.ToString().Contains("/Sitefinity/SignOut"))
    {
        IsUserLoggingOut = true;
    }
    if (IsUserLoggingOut && SystemManager.CurrentHttpContext.Session != null)
    {
        SystemManager.CurrentHttpContext.Session.Remove("Quote");

        IsUserLoggingOut = false;
    }
}

Looks like Sitefinity has its own SystemManager to access the http context. It worked perfectly.

Upvotes: 2

chrisg
chrisg

Reputation: 76

That's pretty close to how I would do it. The only change I would make is change your url comparison logic to be something like:

if (((System.Web.HttpApplication)(sender)).Request.Url.ToString().EndsWith("/Sitefinity/Login/DoLogout"))

Or potentially use .Contains() instead of EndsWith() -- not sure if there are any query-string parameters or trailing slashes added on the DoLogout action.

This is because Request.Url returns a URL (ex. https://stackoverflow.com/whatever) whereas Server.MapPath() returns a local path (ex. C:\inetpub\wwwroot\whatever), so you wouldn't be comparing apples to apples if you're comparing the two.

Edit: Something like this should work, just adding a check to see if the session is null

protected void Application_PostAcquireRequestState(object sender, EventArgs e)
{
    if (((HttpApplication)(sender)).Request.Url.ToString().Contains("sign_out=true"))
    {
        if (HttpContext.Current.Session != null && HttpContext.Current.Session["Cart"] != null)
        {
            HttpContext.Current.Session.Remove("Cart");
            HttpContext.Current.Session["Cart"] = new List<IQuoteResult>();
        }
    }
}

Upvotes: 1

Related Questions