<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>eugene.K &#187; session</title>
	<atom:link href="http://ekdd.co.il/tag/session/feed/" rel="self" type="application/rss+xml" />
	<link>http://ekdd.co.il</link>
	<description>web developer to web developers</description>
	<lastBuildDate>Tue, 22 Sep 2009 06:05:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>HOWTO: Access Session outside of the page’s live cycle</title>
		<link>http://ekdd.co.il/access-session-out-of-page/</link>
		<comments>http://ekdd.co.il/access-session-out-of-page/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 16:43:48 +0000</pubDate>
		<dc:creator>eugene</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[session]]></category>

		<guid isPermaLink="false">http://ekdd.co.il/?p=73</guid>
		<description><![CDATA[You cannot access Session object or any other object which resides in page’s life cycle from out of the scope functions .This follow OOP rules but sometimes complicate development process when you need to access session from external source. In this example i will examine the way i use at any project to hack OOP [...]]]></description>
			<content:encoded><![CDATA[<p>You cannot access Session object or any other object which resides in <a title="asp.net page live cycle explanation" href="http://msdn.microsoft.com/en-us/library/ms178472.aspx" target="_blank">page’s life cycle</a> from out of the scope functions .This follow OOP rules but sometimes complicate development process when you need to access session from external source. In this example i will examine the way i use at any project to hack OOP for my needs. Maybe there is a better solution but this is the best i’ve found so far.</p>
<p><span id="more-73"></span></p>
<p><strong>real life example:</strong></p>
<p>There was a website that used <em>userID </em>stored in the session in order to retrieve information about logged user. All fine because most of the time access to session object comes from within page life cycle ( ie.<em>Page_Load</em>) itself but it has certain complication when it comes to using functions whether these are on the same page but outside of page life cycle scope or inside of <em>App_Code</em> directory.</p>
<p>Why would i access session outside of the page’s life cycle ? Simply because anytime website requests an  <em>userID </em>i need to check session for null in case user lost session or never logged in. Another reason could be architectural change in the application that won’t use <em>userID </em>stored in the session but in object of some kind. So much more elegant solution will be to use external function that does all the job for me. Gets an <em>userID </em>in case there is one and if not gets n0ne can be used in conditional logic for example to redirect an user to login page.</p>
<p>So i’ve created a private function to get an <em>userID </em>and if <em>redirectToLogin </em>variable is <em>TRUE </em>then it redirects. Second private functions retrieves the real <em>userID </em>by accessing current context <em>HttpContext.Current. </em>Same way you can access anything from your page life cycle’s context ( <em>ie.Request </em>and <em>Response) </em>which i use in redirection <em>redirectToLoginPage </em>method. Most important is to understand that this kind of programming gives you flexibility in the future (ie. if you change your login page name or will start storing whole user details in <em>session </em>as<em> User object</em>, this way you will just need to change one function that will access <em>User </em>object stored in session rather searching and modifying every place <em>userID </em>session variable was stored.)</p>
<p><strong>code:</strong></p>
<pre class="brush: c#">

public static int getUserID(bool redirectToLogin)
{
int userID = getUserID();
if (redirectToLogin &amp;amp;&amp;amp; userID == 0)
{
redirectToLoginPage();
}
return userID;
}

private static int getUserID()
{
int userID = 0;
if (HttpContext.Current.Session[&quot;userID&quot;] != null)
{
userID = Convert.ToInt32(HttpContext.Current.Session[&quot;userID&quot;]);
}
return userID;
}

private static void redirectToLoginPage()
{
HttpContext.Current.Response.Redirect(&quot;~/login.aspx&quot;);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ekdd.co.il/access-session-out-of-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
