【问题标题】:Retain the session value when browser is opened for the second time第二次打开浏览器时保留会话值
【发布时间】:2014-03-25 00:34:05
【问题描述】:

第一次登录asp.net应用,保存了一些会话值

  Eg: Session["Test"]="Saving Sesison";

退出应用程序

第二次打开浏览器时,需要保持相同的会话值。

  Eg: Session["Test"]="Saving Sesison";

我该怎么做,谁能帮我一些解决方案以进一步进行。

if (!Page.IsPostBack)
{
  if (Session["Test"] == null)      
                {
                    Binding data to repeater control(with out filter)
                }
                else
                {
                    //Get Session value (To maintain session value across the browser)
                    var cookieSession = Request.Cookies["Test"];      //While opening   the browser for the 2nd time,this line is getting null for all the browsers,but session is getting value for firefox & Chrome not for IE { Session["Test"] }
                if (cookieSession != null &&!String.IsNullOrEmpty(cookieSession.Value))
                    {
                        Session["Test"] = cookieSession.Value;
                    }
                    Binding data to repeater control(with filter using session value)
                }

}

//On Drop down selection.

protected void Dropdown_SelectedIndexChanged(object sender, EventArgs e)
    {
       Binding data to repeater control(based on the dropdown selected value)

Session["Test"] = Dropdown.SelectedItem.Text.ToString(); //To maintain the Dropdown selection all over the app
       // Set it
       if (Session["Test"] == null)
       {
           Session["Test"] = Guid.NewGuid().ToString();
           var cookie = new HttpCookie("Test", (string)Session["Test"]);
           Response.Cookies.Add(cookie);
       }

    }

【问题讨论】:

    标签: asp.net session-cookies


    【解决方案1】:

    ASP.NET 会话范围仅适用于特定会话。所以不可能有那种功能。

    但是您可以以相同的方式使用缓存,它会一直存在,直到您将其设为 null 或超过时间段。但请注意,它适用于每个浏览器。所以要么你需要使用不同的密钥(唯一密钥)而不是'test'

    【讨论】:

      【解决方案2】:

      您有几个选择。虽然会话应该在重新启动的浏览器之间保持粘性,假设它不是处于私有/隐身模式。如果您发现会话超时太快,您可以在 Web.config 中扩展它

      <system.web>
          <sessionState timeout="10080" mode="InProc" />
      </system.web>
      

      timeout 以分钟为单位。注意:如果您正在调试,则停止和启动调试器将重置您的会话。在 IIS 上重新部署应用程序也是如此。如果这对您来说是个问题,您应该使用 SQL 会话状态提供程序之类的东西进行检查:http://msdn.microsoft.com/en-us/library/vstudio/h6bb9cz9(v=vs.100).aspx

      解决此问题的另一种方法是将某种令牌存储在 cookie 中(同样,仅在浏览器未处于隐身/私有模式且未刷新用户数据时才有效)。

      // Set it
      if (Session["Test"] == null)    
      {
          Session["Test"] = Guid.NewGuid().ToString();     
         
          var cookie = new HttpCookie("Test", (string)Session["Test"]);
          Response.Cookies.Add(cookie);
      }
      
      
      // Get it
      var cookieSession = Request.Cookies["Test"];
      
      if (cookieSession != null && !String.IsNullOrEmpty(cookieSession.Value))
      {
          Session["Test"] = cookieSession.Value;
      }
      

      请注意,使用 SQL 会话状态提供程序虽然是更持久的存储之一,但可能会产生一些严重的开销要求。很容易就可以跟踪几场值得跟踪的演出。

      根据我的经验,如果您需要非常确定网站上的某些内容会影响用户体验,那么 cookie 和会话提供程序的组合似乎效果最好。

      编辑

      因此,您的下拉选择保护程序的问题是它始终为 false,并且永远不应该设置 cookie。

      protected void Dropdown_SelectedIndexChanged(object sender, EventArgs e)
      {
          //Binding data to repeater control(based on the dropdown selected value)
      
          // add to Session
          Session["Test"] = Dropdown.SelectedItem.Text.ToString(); 
          
          // Add Cookie
          var cookie = new HttpCookie("Test", (string)Session["Test"]);
          Response.Cookies.Add(cookie);
      }
      

      现在要取回您的数据,请将此代码放入操作/控制器中以在您尝试访问 Session["Test"] 之前运行

      var cookieSession = Request.Cookies["Test"];
      
      if (cookieSession != null && !String.IsNullOrEmpty(cookieSession.Value))
      {
          Session["Test"] = cookieSession.Value; // Should contain the selected text from the drop down
      }
      

      【讨论】:

      • 感谢您的回复,将尝试您的解决方案
      • 这可以在 Firefox 和 Chrome 中使用,但不能在 IE 中使用。您能帮我在 IE 上也使用它吗。企业客户大部分时间都会使用 IE。
      • 很奇怪,它在 IE 中不起作用,这并没有使用自互联网开始以来浏览器不支持的任何东西。哪个部分不起作用,会话和cookie?如果两者都是,您可以在 IE 开发工具中检查它是否在站点的响应中设置了 cookie?
      • 在代码中的任何位置,您都在调用Session.Abandon();Session.Clear(); 或类似的东西吗?
      • 不,我没有清除任何会话和 cookie。两者都不起作用。这就是我感到困惑的原因,它在 Firefox 和 chrome 中工作,但在 IE 中不工作。(我使用的是 IE 8)跨度>
      猜你喜欢
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      相关资源
      最近更新 更多