使用ASP.NET 2.0处理过期会话的最有效方法是什么 [英] What is the most efficient way to handle en expired session with ASP.NET 2.0

查看:109
本文介绍了使用ASP.NET 2.0处理过期会话的最有效方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们正在构建的站点上.我们需要能够在用户会话结束后将其重定向到默认页面.

On the site we are building. We need to be able to redirect the user to a default page when his session has ended.

乍一看,我们将Session_End与Response.Redirect结合使用来完成这项工作.

At first sight we used the Session_End with a Response.Redirect to do this job.

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
    Response.Redirect("~/global/exit.aspx")
End Sub

但是它会产生响应错误的错误消息.自然,我们不想向我们的服务器错误日志发送垃圾邮件.

But it generates a crapload of Response is not available in this context errors. Naturally we don't want to spam our servers error logs.

处理以ASP.NET 2.0结尾的会话的最有效方法是什么?

What is the most efficient way to handle session ending with ASP.NET 2.0?

推荐答案

我们将以下代码添加到global.asax.cs文件中:

We added the following code to the global.asax.cs file:

 private void IsAuthenticated()
    {
        string vFileName = Path.GetFileName(HttpContext.Current.Request.Path);
        string vExt = Path.GetExtension(vFileName).ToLower();
        if ((vFileName != "Login.aspx") && (vExt == ".aspx"))
        {
            if (HttpContext.Current.Session["LoggedIn"] == null)
            {
                HttpContext.Current.Response.Redirect("~/Login.aspx");
            }
        }
    }
    void Application_PostAcquireRequestState(object sender, EventArgs e)
    {
        IsAuthenticated();
    } 

NS:Global .asax文件中的第一行是:

NS: The first line in our Global .asax file is :

<%@ Application  Inherits="???.Global" Language="C#" %>

这篇关于使用ASP.NET 2.0处理过期会话的最有效方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆