Get Session to expire gracefully in ASP.NET

后端 未结 5 1929
南旧
南旧 2020-12-30 11:09

I need a way to tell ASP.NET \"Kill the current session and start over with a brand new one\" before/after a redirect to a page.

Here\'s what I\'m trying to do:

5条回答
  •  盖世英雄少女心
    2020-12-30 11:26

    The adding the cookie trick worked for me also, as follows:

        Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a new session is started        
        If Session.IsNewSession Then
            'If Not IsNothing(Request.Headers("Cookie")) And Request.Headers("Cookie").IndexOf("ASP.NET_SessionId") >= 0 Then
            If Not IsNothing(Request.Headers("Cookie")) AndAlso Request.Headers("Cookie").IndexOf("ASP.NET_SessionId") >= 0 Then
                'VB code
                Dim MyCookie As HttpCookie = New HttpCookie("ASP.NET_SessionId")
                MyCookie.Expires = System.DateTime.Now.AddDays(-1)
                Response.Cookies.Add(MyCookie)
    
                'C# code
                'HttpCookie mycookie = new HttpCookie("ASP.NET_SessionId");    
                'mycookie.Expires = DateTime.Now.AddDays(-1);    
                'Response.Cookies.Add(mycookie);
    
                Response.Redirect("/timeout.aspx")
            End If
        End If       
    End Sub
    

提交回复
热议问题