如何在母版页或 global.asax 中启动时检查会话

发布于 2024-10-11 21:32:38 字数 725 浏览 4 评论 0原文

我是 ASP.NET 表单身份验证和会话的新手,

我想知道如何在母版页或 global.asax 中保存会话 以及如何清除会话

如何通过重定向到页面来更好地处理会话超时

这是我的母版页中的 web.config 会话设置

<sessionState mode="InProc" cookieless="false" timeout="1"></sessionState>

代码

 if (Request.Url.AbsolutePath.EndsWith("SessionExpired.aspx", StringComparison.InvariantCultureIgnoreCase))
    {
        HtmlMeta meta = new HtmlMeta();
        meta.HttpEquiv = "Refresh";
        meta.Content = "7; URL=./Login.aspx";
        Page.Header.Controls.Add(meta);
    }
    else
        HttpContext.Current.Response.AppendHeader("Refresh", Convert.ToString((Session.Timeout * 60)) + "; Url=./Public/SessionExpired.aspx");

i am new in asp.net form authentication and sessions

i would like to know how to save session in masterpage or in global.asax
and how to clear session

how to better handle session timeout by redirecting to a page

this is my web.config session settings

<sessionState mode="InProc" cookieless="false" timeout="1"></sessionState>

code in my masterpage

 if (Request.Url.AbsolutePath.EndsWith("SessionExpired.aspx", StringComparison.InvariantCultureIgnoreCase))
    {
        HtmlMeta meta = new HtmlMeta();
        meta.HttpEquiv = "Refresh";
        meta.Content = "7; URL=./Login.aspx";
        Page.Header.Controls.Add(meta);
    }
    else
        HttpContext.Current.Response.AppendHeader("Refresh", Convert.ToString((Session.Timeout * 60)) + "; Url=./Public/SessionExpired.aspx");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠书 2024-10-18 21:32:38

因此,您的策略对我来说看起来不错,但我更喜欢不同的实现:

  1. 使用从 System.Web.UI.Page 派生的抽象类作为所有页面的基类。假设我们将此类称为 BasePage。
  2. 添加虚拟方法,例如 IsRefreshHeaderNeeded - 默认实现将返回 true。该方法将在 BasePage 的 PreRender 阶段调用,以添加实际的刷新标头作为响应。
  3. 在 SessionExpired 页面中,IsRefreshHeaderNeeded 将被覆盖以返回 false。 (在诸如登录或不需要会话支持的页面等页面中可能需要类似的操作)。
  4. 刷新标题/元以重定向到登录页面将添加在 SessionExpired 页面本身中(实际上它是页面内的逻辑,为什么将其放在全局位置)。 SessionExpired 页面当然会有用于手动导航到登录页面的链接(以防重定向不起作用)。

母版页是一个内容模板,我更喜欢在母版页中仅包含与该内容相关的逻辑。

另一种策略是不使用客户端刷新来处理会话过期,而是当您指示用户返回站点时当前会话已过期时,从服务器端进行刷新。进一步扩展,您甚至可以实现将关键会话数据保存到数据库中,以便您可以重建会话,并且从用户体验的角度来看,不会有会话过期。

As such, your strategy looks OK to me but I would have preferred a different implementation:

  1. Use an abstract class derived from System.Web.UI.Page as a base class for all pages. Let's say we call this class as a BasePage.
  2. Add a virtual method such as IsRefreshHeaderNeeded - the default implementation will return true. The method will be invoked in PreRender stage of BasePage to add actual refresh header in response.
  3. In SessionExpired page, IsRefreshHeaderNeeded will be overridden to return false. (Similar can be needed in pages such as login or pages that don't need session support).
  4. Refresh header/meta to redirect to login page will be added in SessionExpired page itself (really its a logic within the page, why to put it at global place). SessionExpired page would of course have link for use to manually navigate to login page (in case redirect does not work).

Master page is a content template and I prefer to have only logic related that that content within the master page.

Yet another strategy is not to use client side refresh for session expiry but rather do it from server side when you dictates that current session has expired when user visits the site back. Extending further, you may even have implementation that save critical session data into database so that you can reconstruct the session and from user experience perspective, there will be no session expiry.

遥远的她 2024-10-18 21:32:38

来手动清除会话

   Session.Abandon();
   System.Web.Security.FormsAuthentication.RedirectToLoginPage();

您可以通过调用“关于会话过期” :
在某处执行此代码(如 VinayC 建议的 MasterPage 或在从 System.Web.UI.Page 派生的类中执行,该类充当所有内容页面的基类(这意味着在所有代码隐藏中更改从 System.Web.UI.Page 的派生)文件从 aspx 页面到自定义类的派生中)

 if (Request.Url.AbsolutePath.EndsWith("SessionExpired.aspx", StringComparison.InvariantCultureIgnoreCase))
    {
        // your html redirect code here
    }
    else
        Response.Redirect("~/Public/SessionExpired.aspx");

You can clear a session manually by calling

   Session.Abandon();
   System.Web.Security.FormsAuthentication.RedirectToLoginPage();

About the session expiration:
Execute this code somewhere (like VinayC suggested, MasterPage or in a class derived from System.Web.UI.Page that serves as a baseclass for all your content pages (meaning change the derivation from System.Web.UI.Page in all you codebehind files from the aspx pages into the derivation of your custom class)

 if (Request.Url.AbsolutePath.EndsWith("SessionExpired.aspx", StringComparison.InvariantCultureIgnoreCase))
    {
        // your html redirect code here
    }
    else
        Response.Redirect("~/Public/SessionExpired.aspx");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文