Sunday, January 11, 2015

C# Session.Abandon() Event Process Not So Intuitive

Unless you understand how all the events are triggered within the webserver, a casual programmer may not necessarily understand the first time they use this method/function.

I was programming the login and logoff process. I use Session.Abandon() to clear the sessions. On the master page, I have the typical welcome user and log off hyperlink which should be cleared when accessing the logoff page. But it does not and remains on the corner of the page. After stepping through the debugger, the session still exists.

This is because the session is not cleared until all transactions have been completed. This is confirmed both on the Microsoft site and Stackoverflow site. I'm sure there is a reason for this.

Abandon vs Clear

The two links do a pretty good job of explaining the differences but it was not completely clear what impact this had on coding. One interesting comment was that they used Clear and Abandon. The counter-comment was that this was redundant.

I found that this is not redundant as the third comment has pointed out. The reason actually is the way I should have executed my logoff process because Clear() will "nullify" all the session values, then Abandon will clear out the session (supposedly the session id) as I would like a different session id if a different user is on.


My Programming Experience

Although I have been programming for several years in the past, I have fallen so far behind on a lot of new practices. I have enough knowledge to probably transition between one programming language to another, but not enough to program without a reference book or revisiting a lot of less-used functionality or program once functionality (ie login and logoff). I probably just used a flag in my past career life.

I had to look up the C# page even orders. I recall this was one of my interview questions. There is a bunch of events, but I typically only had to worry about init and load so I probably just answered that question poorly (mediocre at best... I knew a couple others but not the specific names).

Reference

http://msdn.microsoft.com/en-us/library/ms524310(v=vs.90).aspx
http://stackoverflow.com/questions/1470445/what-is-the-difference-between-session-abandon-and-session-clear

No comments:

Post a Comment