【问题标题】:Manage Session in the web config file using ASP.net使用 ASP.net 在 web 配置文件中管理会话
【发布时间】:2017-11-22 20:53:59
【问题描述】:

我的代码中有两个会话:

用于用户登录验证的会话["Email"]

Session["prevUrl"] 记住上次访问的页面

我想知道如何在我的 webconfig 文件中管理会话超时。我只希望 Session["Email"] 超时,以便用户可以重定向到他们重新登录时访问的最后一个页面。目前两个会话都在 1 分钟后超时

谢谢

Web.config

 <sessionState mode="InProc" customProvider="DefaultSessionProvider" timeout="1">

</sessionState>

Global.asax.cs

void Session_Start(object sender, EventArgs e)
{
        // Code that runs when a new session is started  
if (Session["prevUrl"] != null)
{
  Response.Redirect((string)Session["prevUrl"]); //Will redirect to previous page visited
}
else
{
  //Redirect to Login Page if Session is null & Expires   
   Response.Redirect("Login.aspx");
}
}

C# 页面加载

 protected void Page_Load(object sender, EventArgs e)
 {
    Session["prevUrl"] = Request.Url;

        if (Session["Email"] == null)
        {
            Response.Redirect("Login.aspx");
        }

 }

【问题讨论】:

    标签: c# asp.net session


    【解决方案1】:

    没有办法强制会话密钥过期。对于这种情况,您必须使用缓存。

    【讨论】:

    • 嗨 Vijay 知道如何使用缓存来实现它。我是新来的。谢谢
    • string userIdentifier = string.Empty; Cache.Add("Key" + userIdentifier, "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
    • 缓存在用户之间共享,这里的用户标识符是用来标识当前用户的。
    猜你喜欢
    • 2016-03-01
    • 2015-02-12
    • 2015-04-14
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2011-01-18
    相关资源
    最近更新 更多