【问题标题】:Sitefinity - Remove Session after LogoutSitefinity - 注销后删除会话
【发布时间】:2015-07-14 18:46:11
【问题描述】:

我正在尝试在用户退出 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

【问题讨论】:

    标签: session global-asax sitefinity


    【解决方案1】:

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

    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 上下文。效果很好。

    【讨论】:

      【解决方案2】:

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

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

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

      这是因为 Request.Url 返回一个 URL(例如 https://stackoverflow.com/whatever)而 Server.MapPath() 返回一个本地路径(例如 C:\inetpub\wwwroot\whatever),所以你不会比较苹果如果您将两者进行比较,则为苹果。

      编辑: 像这样的东西应该可以工作,只需添加一个检查以查看会话是否为空

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

      【讨论】:

      • 原来可以在Application_AuthenticateRequest中访问Session
      • 我已经更新了一些新代码和错误堆栈跟踪..不确定您是否可以提供帮助
      • 知道空引用异常发生在哪里吗?是 Request.Url 吗?还是 HttpContext.Current.Session?无论哪种方式,我都会对其进行调试并找出哪个为空,然后在该违规行之前添加一个'!= null'
      • 这是 Session,因为我删除了 Request.Url IF 语句,只是检查了 Session 是否 != null
      • 感谢您的帮助。我以前看过那个帖子。我认为 sitefinity 有点时髦。
      猜你喜欢
      • 2015-01-14
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 2012-09-28
      • 2019-03-16
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      相关资源
      最近更新 更多