Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在重定向到会话结束事件的另一页_C#_Asp.net - Fatal编程技术网

C# 正在重定向到会话结束事件的另一页

C# 正在重定向到会话结束事件的另一页,c#,asp.net,C#,Asp.net,我想在会话超时时自动重定向到登录页面 在web.config文件中,我有以下代码 <configuration> <system.web> <sessionState mode="InProc" timeout="1"/> </system.web> </configuration> 但超时后,我收到以下错误: 用户代码未处理HttpException 响应在此上下文中不可用 有什么线索可以解决这个问题

我想在会话超时时自动重定向到登录页面

在web.config文件中,我有以下代码

<configuration>
    <system.web>
       <sessionState mode="InProc"  timeout="1"/>
    </system.web>
</configuration>
但超时后,我收到以下错误:

用户代码未处理HttpException

响应在此上下文中不可用


有什么线索可以解决这个问题吗?

我认为您得到的是“响应在此上下文中不可用”,因为用户没有向服务器发出请求,因此您无法向服务器提供响应。请尝试使用Server.Transfer。

如果您正在使用类似于
的FormsAuthentication
来维护应用程序的安全性,则此部分(您正在尝试的部分)将为您完成。如果FormsAuthentication发现用户的会话已过期,它会将用户重定向回您的登录页面


其次,不要过分依赖会话结束,因为如果您将会话提供程序从InProc更改为SQLServer或其他进程外提供程序,它将不会触发。

会话结束时调用会话结束-通常是在最后一个请求后20分钟(例如,如果浏览器处于非活动状态或关闭状态)。
因为没有请求,所以也没有响应

如果没有活动会话,我建议在
应用程序中执行重定向。记住通过检查当前url来避免循环

编辑:我不喜欢.net内置的身份验证,例如
Global.asax

    protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        try
        {
            string lcReqPath = Request.Path.ToLower();

            // Session is not stable in AcquireRequestState - Use Current.Session instead.
            System.Web.SessionState.HttpSessionState curSession = HttpContext.Current.Session;

            // If we do not have a OK Logon (remember Session["LogonOK"] = null; on logout, and set to true on logon.)
            //  and we are not already on loginpage, redirect.

            // note: on missing pages curSession is null, Test this without 'curSession == null || ' and catch exception.
            if (lcReqPath != "/loginpage.aspx" &&
                (curSession == null || curSession["LogonOK"] == null))
            {
                // Redirect nicely
                Context.Server.ClearError();
                Context.Response.AddHeader("Location", "/LoginPage.aspx");
                Context.Response.TrySkipIisCustomErrors = true;
                Context.Response.StatusCode = (int) System.Net.HttpStatusCode.Redirect;
                // End now end the current request so we dont leak.
                Context.Response.Output.Close();
                Context.Response.End();
                return;
            }
        }
        catch (Exception)
        {

            // todo: handle exceptions nicely!
        }
    }

您可以使用会话属性
IsNewSession
来检测它是否为会话超时

ASP.NET
HttpSessionState
类具有
IsNewSession()
方法,如果为此请求创建了新会话,该方法将返回
true
。检测会话超时的关键是在请求中查找
ASP.NET\u SessionId
cookie

当然,我也同意我们应该将下面的代码放在一些所谓的定制基本页面中,所有页面都使用它来有效地实现这一点

override protected void OnInit(EventArgs e)
  {
       base.OnInit(e);   

if (Context.Session != null)
   {
if (Session.IsNewSession)
    {

     string CookieHeader = Request.Headers["Cookie"];
     if((CookieHeader!=null) && (CookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
     {
      // redirect to any page you need to
      Response.Redirect("sessionTimeout.aspx");
     } 
   }
 }
}

如果您想将上述代码放入一个基本页面类中,请勾选此项以获取更多解释。

您应该使用应用程序\u equirestate 您将发现应用程序\u AuthenticateRequest不再具有会话上下文(或从未有过会话上下文)。 在我对这篇文章的研究中,我看到了这篇文章,它为我解决了这个问题。 感谢Waqas Raja


您只需在web.config中执行以下操作即可

<configuration>
<system.web>
<sessionState mode="InProc"  timeout="1" loginurl="destinationurl"/>
</system.web>
</configuration>


因为,我们无法从会话\u End重定向,因为那里没有响应/重定向。通过使用此选项,当会话超时时,您将被重定向到destinationurl。希望这能有所帮助。

我觉得最简单的方法是使用元信息并使技巧发挥作用。考虑到我们有一个页面<代码>网页。ASPX 在下面的代码中添加下面的代码。
private void Page_Load(object sender, System.EventArgs e){      
    Response.AddHeader("Refresh",Convert.ToString((Session.Timeout * 60) + 5));      
    if(Session[“IsUserValid”].ToString()==””)              
    Server.Transfer(“Relogin.aspx”);
}

在上面的代码中,
WebPage.aspx
在会话过期5秒后刷新。并在页面加载中验证会话,因为会话不再有效。该页面将重定向到重新登录页面。每次发回服务器的帖子都会刷新会话,并在
网页的元信息中更新会话。aspx

您应该在身份验证元素中执行此操作。。不在代码中。当您说会话超时时重定向到登录页面时,您的意思是希望浏览器(客户端)从服务器事件自动触发重定向吗?如果是这样,这将需要的不仅仅是服务器代码,更像是使用信号集线器触发客户端javascript重定向。据我所知,
Session\u End
事件不会作为请求的一部分触发。当超时发生时,它将在后台触发。下次用户返回时,他将自动获得一个新会话,因为旧会话已超时。因此,当
会话结束
事件发生时,没有可以重定向的当前
响应
。这可能会有所帮助?我还尝试了“Server.Transfer(“~/NewPage.aspx”,false);”。但是不起作用。错误“Object reference not set to a instance of a Object.”嗯,我想我应该实现一个javascript计时器,将其设置为与会话超时相同的长度,然后在超时时使用window.location重定向它们。计时器会在页面重新加载时刷新,就像他们的会话一样。作为额外的奖励,您可以在用户的会话即将到期时警告用户。请提供一个片段,好吗?“我不是asp.net中的高级程序员。@SKPaul添加了示例,但您应该正确地测试并理解代码,并在将其用于生产之前完全理解其后果!”!但这当然适用于任何解决方案。无需关闭浏览器(最终)触发会话结束。用户只需在超时期间处于非活动状态(而不是发出请求)。这可能是因为浏览器已关闭、用户已导航离开、他午休或忙于阅读(长?)页面长达20分钟。@HansKesting是的,当然,已编辑。这是一个澄清不可能发出重定向的例子。这个答案措词不正确。形式认证和会话是分开的。您可以拥有有效的forms auth票证,但也可以拥有即将到期的会话。也就是说,对于提供的只是重定向到登录页面的内容,表单身份验证将执行重定向是正确的。见:
private void Page_Load(object sender, System.EventArgs e){      
    Response.AddHeader("Refresh",Convert.ToString((Session.Timeout * 60) + 5));      
    if(Session[“IsUserValid”].ToString()==””)              
    Server.Transfer(“Relogin.aspx”);
}