IE seems a bit buggy when it comes on Abondoning the session. This is the whole code thats get executed:
Protected Sub logout_OnClick(ByVal sender As Object, ByVal e As EventArgs)
Session.Abandon()
Response.Redirect("login.aspx")
End Sub
It redirects to login.aspx but when i change the url to default.aspx its get in without checking it. In all the other browsers it doesnt and get you redirected because of the following code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If (Session("Naam") Is Nothing) Then
Response.Redirect("login.aspx")
Else
Label1.Text = "Welkom " + Session("Naam").ToString()
End If
End Sub
Is there any reason that IE doesnt abandon the session?
note//
I am not using log-incontrol what so ever
Try disabling/removing the autopostback and then re-test your page. I would guess that the autopostback is somehow keeping your session data active,maybe its being used in the postback? therefore all instance's dont get abandoned?.
Edit
When the Abandon method is called, the current Session object is queued for deletion but is not actually deleted until all of the script commands on the current page have been processed. This means that you can access variables stored in the Session object on the same page as the call to the Abandon method but not in any subsequent Web pages.
For example, in the following script, the third line prints the value Mary. This is because the Session object is not destroyed until the server has finished processing the script.
<%
Session.Abandon
Session("MyName") = "Mary"
Reponse.Write(Session("MyName"))
%>
If you access the variable MyName on a subsequent Web page, it is empty. This is because MyName was destroyed with the previous Session object when the page containing the previous example finished processing.
The server creates a new Session object when you open a subsequent Web page, after abandoning a session. You can store variables and objects in this new Session object.
The above is from the MSDN website.
Which all means that somewhere your session object is being used AFTER you have abandoned it.
I normally use the same code block as you do to log a member out of a session.
Session.Abandon()
Response.Redirect("default.aspx")
with the same code in the global.asax or global.aspx file too.
Related
I have a session which contains a List(of String) which I create on Main.aspx. In my Global.asax I execute the following code to retrieve the Session value after a Timer is fired.
Private Sub Application_AcquireRequestState(sender As Object, e As EventArgs)
If System.Web.HttpContext.Current.Session IsNot Nothing Then
If Session("Products") IsNot Nothing Then
Response.Write(Session("Products").ToString())
End If
End If
End Sub
However, Session("Products") returns Null and the Sub is exited. I wonder if End Sub in Main.aspx releases the Session and therefore whenever the Timer fires, the Session is empty. Any thoughts?
In your Main.vb code behind, be sure to inherit the System.Web.SessionState.IRequiresSessionState interface in addition to whatever is being inherited.
Something like this should work:
Public Class Main
Inherits System.Web.UI.Page, System.Web.SessionState.IRequireSessionState
' Your application code
End Class
This is strange behavior though as ASP.NET webpages should already handle session data. Usually, you inherit IRequireSessionState on generic handlers (.ashx) since they don't support session data by default.
I'm trying to store the session state in a master page to keep track of the previous URL. Here's what I'm doing
Public Property PreviousPage() As String
Get
Return Session("theprevpage")
End Get
Set(value As String)
Session("theprevpage") = value
End Set
End Property
Private Function HandleSiteNode(ByVal sender As Object, ByVal e As SiteMapResolveEventArgs) As SiteMapNode
Dim currNode As SiteMapNode = SiteMap.CurrentNode.Clone(True)
Dim tempNode As SiteMapNode = currNode
Dim strPrev As String = PreviousPage
' Append parent pages query string back onto the parent's node URL
If Not tempNode.ParentNode Is Nothing Then
If strPrev.Contains("?") Then
tempNode.ParentNode.Url = tempNode.ParentNode.Url + "?" + strPrev.Split("?")(1)
End If
End If
Return currNode
End Function
And in the master page load function
If Not IsPostBack Then
AddHandler SiteMap.SiteMapResolve, AddressOf HandleSiteNode
PreviousPage = Request.UrlReferrer.ToString()
End If
Now, here is where it gets strange.
The first page is a login page the master load doesn't get called on. After I log in it then going to the main.aspx page, and it successfully saves the "login.aspx" page in the session state.
Now, when I go to navigate the 2nd time after logging in, the session state is set successfully, but by the time it gets into the HandleSiteNode which is called after the session was set successfully, the session still says the url is "login.aspx" and not "main.aspx"
No where else in the code am I setting this session state, it just seems to revert back to its previous value on its own.
No matter how many links I click & how many times the session is set, the Session variable will never change to anything else besides "login.aspx"
Help!
edit: Another odd detail, when I move the AddHandler line from master page into a non-master page, the session state is saved properly. However, if I try to move the Addhandler code into the Master page MainContent.Load function, it still doesn't work
SiteMapResolve is a static event.
This means that it doesn't have access to the session object. You'll note this if you put a breakpoint in your HandleSiteNode code and inspect the Session.SessionId property.
The examples on MSDN about the event all target the global.asax file which means that handler is really geared towards a single use of the site. Note that the MSDN example I linked to is a little jacked in that it attaches a new event on every page load, which will eat memory. The event should only be attached to once.
Click here for more info on potential ways to get around the issues.
Apparently, my web project has at least 2 different session states.
I can access the session state that contains the value I want by using
e.Context.Session("theprevpage")
This seems like a bit of a hack but it's working for me.
Sounds easy, right? Here's the scenario...
Private dbQuery As New ReefEntities
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
CurrentCoral = (From c In dbQuery.Corals Where c.CoralID = intCoralID).FirstOrDefault
txtCommonName.Text = CurrentCoral.CommonName
End If
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
'how do I access the coral from the page load to update it?
CurrentCoral.CommonName = strName
dbQuery.SaveChanges()
End Sub
I don't want to re-query my result, I want to update the query from the page load and then save the changes, right? How do I access that original object to update it?
HTTP is a stateless protocol and as a result, every request you make to your server needs to rebuild your object graph unless you persist it somewhere. There are many ways to persist data across a web "session". In ASP.NET you can store data in cookies, server side session, viewstate, form variables, and more.
First you would detach your CurrentCoral from the object context when you're done with it in Page_Load
dbQuery.Detach(CurrentCoral)
Then put it in a data store like view state.
Me.ViewState.Add("CurrentCoral", CurrentCoral)
In the next web request when your save button is clicked, retrieve the entity from the view state and attach it to your new object context.
CurrentCoral = CType(Me.ViewState("CurrentCoral"), Coral)
dbQuery.Attach(CurrentCoral)
CurrentCoral.CommonName = strName
dbQuery.SaveChanges()
Please forgive any syntax errors. VB.NET is not my first language! For more details on attaching and detaching entities with Entity Framework see the following article.
Attaching and Detaching Objects
You could put your CurrentCoral object into the ViewState or Session, then retrieve it in the Click event.
Dim oCurrentCorral as Object = ViewState("CurrentCoral")
dbQuery.ObjectStateManager.ChangeObjectState(oCurrentCoral, System.Data.EntityState.Added)
dbQuery.SaveChanges()
If the request is a postback, the record isn't loaded in Page_Load at all – there's nothing to re-query since you haven't made a query in the request at all. I'd say to just re-query in the postback handler, chances are the overhead of hitting your database once to update is much smaller than the overhead of sending the whole entity serialised in ViewState all the time.
I'm using some session variables to store and pass data across several pages of an ASP.Net application. The behavior is a bit unpredictable though. I'm setting the session variable post page_load as follows
Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
If (Session.Item("ScholarshipID") = Nothing) Then
Session.Add("ScholarshipID", "Summer2011")
End If
Ok so far so good, on a normal page load. If the user completes the form action, hits the next page, and decides OH NO, i needed to change field xyz, and clicks back, corrects the data, then submits, the session variable is showing as NULL. Why would a cached session behave this way? I'm not destroying/clearing the variable, unless I fail to understand the scope of session variables.
try
If (IsNothing(Session("Scholarship"))) Then
Session("Scholarship") = "Summer2011"
End If
I have a user control that checks if a certain query string and session value are present then returns a boolean based on that, if it's true I want to set the master page.
The page is throwing an Object reference exception when it tries to call the method EditUser1.UserAuthorization(). Why is this happening? I imagine that the method doesn't exist at that point in the stack.
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
If EditUser1.UserAuthorization(True) Then
Page.MasterPageFile = "APERSEmpCont.master"
End If
End Sub
I just found out from here, that the page controls haven't been initialized at the point of the preinit, so that method doesn't exist at that moment. I'll have to move the method to the page level to make it work.