Sitefinity - 注销后删除会话

Sitefinity - Remove Session after Logout

我正在尝试在用户退出 Sitefinity 页面后清除 HttpContext.Current.Session。

我在这个 link 中看到您可以检查 Request.Url 但我不太确定实现是什么。

这是我目前的尝试:

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>();
    }
}

如果您有任何提示或建议,或者我的逻辑完全错误,请告诉我。

提前致谢。

更新:

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>();
            }
        }
    }

这是我下一次尝试完成同一任务,但我一直收到 NullReferenceException...

注意:我在Application_AcquireRequestState方法中也试过这个方法

这是堆栈:

[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

这与我的做法非常接近。我要做的唯一更改是将您的 url 比较逻辑更改为:

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

或者可能使用 .Contains() 而不是 EndsWith() -- 不确定在 DoLogout 操作中是否添加了任何查询字符串参数或尾部斜杠。

这是因为 Request.Url returns 一个 URL(例如 https://whosebug.com/whatever)而 Server.MapPath() returns 一个本地路径( ex. C:\inetpub\wwwroot\whatever), 所以如果你比较两者,你就不会把苹果与苹果进行比较。

编辑: 像这样的东西应该可以工作,只需添加一个检查以查看会话是否为 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>();
        }
    }
}

这最终成为我的解决方案:

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;
    }
}

看起来 Sitefinity 有自己的 SystemManager 来访问 http 上下文。效果很好。