<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Don't redirect after setting a Session variable (or do it right)</title><link>http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx</link><description>A problem I see over and over again on the ASP.NET forums is the following: In a login page, if the user and password have been validated, the page developer wants to redirect to the default page. To do this, he writes the following code: Session[&amp;quot;Login&amp;quot;</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: Don't redirect after setting a Session variable (or do it right)</title><link>http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx#7247203</link><pubDate>Wed, 04 Nov 2009 19:31:19 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7247203</guid><dc:creator>Bertrand Le Roy</dc:creator><author>Bertrand Le Roy</author><description>&lt;p&gt;@etam: no, except if transfer was the right thing to do to begin with. They are not interchangeable.&lt;/p&gt;
&lt;p&gt;Do a redirect (with the right overload) when you want the URL in the browser to reflect the change, and do a transfer if you want to delegate the processing of the current request to another handler or page without it being reflected in the URL. Those are two very different use cases and one should not switch just to work around a bug. Fix the bug instead :)&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7247203" width="1" height="1"&gt;</description></item><item><title>re: Don't redirect after setting a Session variable (or do it right)</title><link>http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx#7247196</link><pubDate>Wed, 04 Nov 2009 19:21:10 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7247196</guid><dc:creator>etam</dc:creator><author>etam</author><description>&lt;p&gt;I&amp;#39;m getting the same problem on production server.on &amp;nbsp;localhost it is working fine . do you suggest to use server.transfer instead?&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7247196" width="1" height="1"&gt;</description></item><item><title>re: Don't redirect after setting a Session variable (or do it right)</title><link>http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx#7246144</link><pubDate>Tue, 03 Nov 2009 20:35:55 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7246144</guid><dc:creator>Bertrand Le Roy</dc:creator><author>Bertrand Le Roy</author><description>&lt;p&gt;@Greg: I'd bet for caching. Probably too aggressive.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7246144" width="1" height="1"&gt;</description></item><item><title>re: Don't redirect after setting a Session variable (or do it right)</title><link>http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx#7246138</link><pubDate>Tue, 03 Nov 2009 20:27:23 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7246138</guid><dc:creator>Greg Vestal</dc:creator><author>Greg Vestal</author><description>&lt;p&gt;I have what appears to be a unique issue which I have pretty much written off to the user&amp;#39;s system setup. &amp;nbsp;We have an apartment application here that works fine for 1000s of users without this problem. &amp;nbsp;Also works in production with anybody else here loggin into their profile. &amp;nbsp;It seems that a session variable is changing between page posts. &amp;nbsp;Basically we have a customer they login, they choose a property and the next page is a list of units in that complex. &amp;nbsp;Well what is happening is the units that come up on the next page are from a different complex. &amp;nbsp;I have never heard of the session variable changing like that. &amp;nbsp;I haven&amp;#39;t checked the system set up yet but was wondering if you have any idea what might be causing this?? &amp;nbsp;This is VS 2002 written in VB.NET.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7246138" width="1" height="1"&gt;</description></item><item><title>re: Don't redirect after setting a Session variable (or do it right)</title><link>http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx#7231588</link><pubDate>Fri, 16 Oct 2009 18:37:02 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7231588</guid><dc:creator>Bertrand Le Roy</dc:creator><author>Bertrand Le Roy</author><description>&lt;p&gt;@Sanjeeb: I don't know but I'd suspect the error redirect is not using the overload that doesn't terminate the response. You might want to consider storing the error in another container than session.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7231588" width="1" height="1"&gt;</description></item><item><title>re: Don't redirect after setting a Session variable (or do it right)</title><link>http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx#7231296</link><pubDate>Fri, 16 Oct 2009 09:56:50 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7231296</guid><dc:creator>Sanjeeb</dc:creator><author>Sanjeeb</author><description>&lt;p&gt;Dear Bertrand,&lt;/p&gt;
&lt;p&gt;I have this in my web.config:&lt;/p&gt;
&lt;p&gt;&amp;lt;customErrors mode=&amp;quot;On&amp;quot; defaultRedirect=&amp;quot;Forms/GenericError.aspx&amp;quot;&amp;gt;&amp;lt;/customErrors&amp;gt;&lt;/p&gt;
&lt;p&gt;And this in my Global.asax:&lt;/p&gt;
&lt;p&gt;void Application_Error(object sender, EventArgs e)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Exception ex = Server.GetLastError().GetBaseException();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;HttpContext.Current.Session[“LastException”] = ex.Message;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;And this in my GenericError.aspx:&lt;/p&gt;
&lt;p&gt;protected void Page_Load(object sender, EventArgs e)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;object lastException = Session[MNPSessionConstant.LastException];&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if (lastException != null)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.lblErrorMessage.Text = (string)lastException;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Session[“LastException”] = null;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Now when I get to the Page_Load of the error page for the first time I always get the session variable as null. But if I click the back button and go to the previous page and the do the same exception throwing activity again, I get to see the session variable value in the error page. &lt;/p&gt;
&lt;p&gt;Could you please help me understand why this is happening?&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Sanjeeb&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7231296" width="1" height="1"&gt;</description></item><item><title>re: Don't redirect after setting a Session variable (or do it right)</title><link>http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx#7160380</link><pubDate>Mon, 03 Aug 2009 16:22:54 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7160380</guid><dc:creator>Bertrand Le Roy</dc:creator><author>Bertrand Le Roy</author><description>&lt;p&gt;@Kenny: you should probably contact support. contact me if you don't know how.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7160380" width="1" height="1"&gt;</description></item><item><title>re: Don't redirect after setting a Session variable (or do it right)</title><link>http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx#7160074</link><pubDate>Mon, 03 Aug 2009 07:27:39 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7160074</guid><dc:creator>Kenny</dc:creator><author>Kenny</author><description>&lt;p&gt;No, a single server running two websites. I don&amp;#39;t know. I&amp;#39;m just tired of looking at the same code for 45 straight hours. A million websites using session variables without incident, and mine is being a pain in the arse. I have to have this running by morning and it&amp;#39;s getting worse, not better!&lt;/p&gt;
&lt;p&gt;But thanks for your help! :-)&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7160074" width="1" height="1"&gt;</description></item><item><title>re: Don't redirect after setting a Session variable (or do it right)</title><link>http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx#7160070</link><pubDate>Mon, 03 Aug 2009 07:19:09 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7160070</guid><dc:creator>Bertrand Le Roy</dc:creator><author>Bertrand Le Roy</author><description>&lt;p&gt;@Kenny: no idea then, sorry. Web farm?&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7160070" width="1" height="1"&gt;</description></item><item><title>re: Don't redirect after setting a Session variable (or do it right)</title><link>http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx#7160059</link><pubDate>Mon, 03 Aug 2009 07:03:41 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7160059</guid><dc:creator>Kenny</dc:creator><author>Kenny</author><description>&lt;p&gt;But I have no static variables. I have noticed something though in the past 30 monutes or so. I created a page called &amp;quot;test.aspx&amp;quot; and it does nothing but display the session data to a label. After I hit the refresh button a few times (usually 3 times), it errors out and says the session object doesn&amp;#39;t exist. What would cause the object to disappear?&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7160059" width="1" height="1"&gt;</description></item></channel></rss>