473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Session variables timing out too soon in ASP 2.0

I created a website in ASP 2.0/c# that connects to an Access 2000 database. Currently I have 4 pages that people can view without logging in, which are reports that post marathon race results. Only one maybe two people will actually ever have to log in to enter the race data.

For the login I created a form that asks for user name and password. Then I do a sql query to see if that matches the information in the database. If it does it grabs their role which would be admin.

Here is my code for this:
Expand|Select|Wrap|Line Numbers
  1.     protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.         if (txtUser.Text != "" && txtPassword.Text != "")
  4.         {
  5.             OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" + Server.MapPath("~/App_Data/pfrpc.mdb"));
  6.             OleDbCommand cmd = new OleDbCommand("select RunnerID, Role from Runner WHERE UserName = @UserName and Pass = @Password", conn);
  7.  
  8.             cmd.Parameters.Add("@UserName", OleDbType.VarChar, 50);
  9.             cmd.Parameters["@UserName"].Value = txtUser.Text;
  10.             cmd.Parameters.Add("@Password", OleDbType.VarChar, 50);
  11.             cmd.Parameters["@Password"].Value = txtPassword.Text;
  12.  
  13.             conn.Open();
  14.  
  15.             OleDbDataReader reader = cmd.ExecuteReader();
  16.             string test = "";
  17.             if (reader.Read())
  18.             {
  19.                 Session["userRole"] = reader["Role"];
  20.                 Session["RunnerID"] = reader["RunnerID"];
  21.  
  22.                 test = Session["userRole"] + "";
  23.  
  24.  
  25.                 if (test == "admin")
  26.                 {
  27.                     Response.Redirect("RaceTimes.aspx");
  28.                 }
  29.  
  30.             }
  31.             else
  32.             {
  33.                 lblError.Text = "Invalid Login";
  34.             }
  35.             reader.Close();
  36.             conn.Close();
  37.  
  38.         }
  39.         else
  40.         {
  41.             lblError.Text = "You must fill in both the username and password fields";
  42.         }
  43.  
  44.     }
  45.  
Once the session variable is equal to admin the other pages become available. By default in all of the admin pages the controls are not visible and if the session variable is equal to admin it will make the items visable. Here is a sample of one of the pages:
Expand|Select|Wrap|Line Numbers
  1.         string test = Session["UserRole"] + "";
  2.  
  3.         //verifies if user logged in
  4.  
  5.         if (test == "admin")
  6.         {
  7.             txtMinutes.Visible = true;
  8.             txtSeconds.Visible = true;
  9.             txtYear.Visible = true;
  10.             ddlBibNum.Visible = true;
  11.             ddlRace.Visible = true;
  12.             lblBibNum.Visible = true;
  13.             lblMinutes.Visible = true;
  14.             lblSeconds.Visible = true;
  15.             lblYear.Visible = true;
  16.             lblRaceNum.Visible = true;
  17.             btnSave.Visible = true;
  18.  
  19.         }
  20.         else
  21.         {
  22.             lblError.Text = "You must login to have access to this page!";
  23.         }
  24.  
Anyways this works on my pc through development but after I put these files on a server (which is on a free hosting server that I am using for testing) it times out really fast. After about 30 seconds to a minute my session loses that it is an admin and the pages tell me that I am not an admin anymore. Any thoughts on this? Are session variables the best way to go in this case? Hopefully I provided enough code for you to have an understanding of what I am trying to do. Any help would be much appreciated!

Thanks!
Aug 6 '07 #1
16 2024
TRScheel
638 Expert 512MB
Curiously, do you ever set the timeout on the Session?
Aug 6 '07 #2
No, I'm not sure how to do that. Where do I set that? In the web.config?
Aug 6 '07 #3
TRScheel
638 Expert 512MB
No, I'm not sure how to do that. Where do I set that? In the web.config?
The session object itself should have a timeout property
Aug 6 '07 #4
Plater
7,872 Expert 4TB
No, I'm not sure how to do that. Where do I set that? In the web.config?
Some session timeout settings are in the IIS settings for the web-application itself.
Aug 6 '07 #5
Well I had time to play with it a bit more. It seems that if I stay in the same page and add data (while refreshing and clearing the form after the data is saved)the session doesn't time out. It seems to loose the session variable when I go from page to page. It has to be the way I coded the pages. Any thoughts on this?
Aug 6 '07 #6
Well I have to change that theory again. After more testing I have found that the pages are timing out. It is just the form because visible and I didn't have them to turn back to invisible if the user wasn't an admin.

At least I found one flaw....

I also emailed my host provider to ask about their servers... Lets see what they say. Does anyone have any other suggestion other then session variables?
Aug 7 '07 #7
Plater
7,872 Expert 4TB
Are you set to use "InProc" as your seesion mode?
Aug 7 '07 #8
Umm I'm not sure. I am using what ever is default for session variables. Here is my web.config:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2.  
  3. <configuration>
  4.     <appSettings/>
  5.     <connectionStrings/>
  6.     <system.web>
  7.  
  8.     <pages theme ="purple" />
  9.  
  10.         <compilation debug="true"/>
  11.  
  12.         <authentication mode="Windows"/>
  13.  
  14.  
  15.  
  16.     <siteMap defaultProvider="AspNetXmlSiteMapProvider" enabled="true">
  17.  
  18.       <providers>
  19.  
  20.         <add siteMapFile="~\web.sitemap" name="MyXmlSiteMapProvider" type="System.Web.XmlSiteMapProvider"/>
  21.         <add siteMapFile="~\web2.sitemap" name="MyXmlSiteMapProvider2" type="System.Web.XmlSiteMapProvider"/>
  22.       </providers>
  23.  
  24.     </siteMap>
  25.  
  26.  
  27.   </system.web>
  28. </configuration>
Aug 7 '07 #9
Frinavale
9,735 Expert Mod 8TB
Anyways this works on my pc through development but after I put these files on a server (which is on a free hosting server that I am using for testing) it times out really fast. After about 30 seconds to a minute my session loses that it is an admin and the pages tell me that I am not an admin anymore. Any thoughts on this? Are session variables the best way to go in this case? Hopefully I provided enough code for you to have an understanding of what I am trying to do. Any help would be much appreciated!

Thanks!
I know this wont help you to understand your Session Time out issues....

But Since you've ask if Session variables are the best way to go in this case I'm going to suggest you use something completely different.

Since you're using .NET 2.0 framework there are tools available to you that simplify the user authentication and authorization process.

Check out this video first.

Then I'd suggest looking into the information provided by MSDN on the following:
The video was taken from a website that contains a lot of useful information on the topic:this website is found here.


I hope this helps you out!

-Frinny
Aug 7 '07 #10
Plater
7,872 Expert 4TB
The Session Timeout and InProc settings are in the IIS management controls, which, you probably don't have access to since you're hosted.
Check with your host, and if they're not already there, have them set the Session Timeout to like 15-20mins (or whatever) and make sure it's set to "InProc".
That might not help, but it's good to check.

My settings:
Session State Mode: "InProc"
Cookieless mode: "UseCookies"
Session Timeout(inutes): "20"


Hmm, is your session cookie being set and passed correctly between pages? That could cause the problem you are seeing as well.
Aug 7 '07 #11
Hello again,
Thanks for everyone's help on this. I contacted the host provider and got a generic message stating to check the tech support page which sucks. So I replied with a more specific question. Lets see if they get back to me with a real answer or not.

Either way I plan on looking into the form authentication this weekend. I have other things going on that I can't spend alot of time on it until then. If I run into anymore problems I will post again.

Thanks!
Aug 9 '07 #12
Well I sort of got the form authentication working except for 1 thing. My sitemaps. I have 2 sitemaps, 1 for anyone and 1 for admins. I don't want the admin sitemap to be visible until the admin logs in. I read to do this I could use securityTrimmingEnabled="true". This does hide the second sitemap but it doesn't bring it back when I log in. I don't know what I am doing wrong here. I know my security is working because if I take out that line the site map appears and if those links are clicked it redirects to the login page. Once logged in those pages are accessible. Any thoughts?

Here is my webconfig:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2.  
  3. <configuration>
  4.     <appSettings/>
  5.     <connectionStrings/>
  6.     <system.web>
  7.         <roleManager enabled="true" />
  8.   <pages theme="purple"/>
  9.  
  10.         <compilation debug="true">
  11.             <assemblies>
  12.                 <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
  13.                 <add assembly="System.Security, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
  14.                 <add assembly="Accessibility, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
  15.                 <add assembly="System.Runtime.Serialization.Formatters.Soap, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
  16.                 <add assembly="System.Deployment, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
  17.  
  18.         <authentication mode="Forms" />
  19.  
  20.  
  21.         <siteMap defaultProvider="AspNetXmlSiteMapProvider" 
  22.     enabled="true">
  23.             <providers>
  24.                 <add siteMapFile="~\web.sitemap" name="MyXmlSiteMapProvider" type="System.Web.XmlSiteMapProvider"  />
  25.         <add siteMapFile="~\web2.sitemap" name="MyXmlSiteMapProvider2" type="System.Web.XmlSiteMapProvider"  />
  26.  
  27.       </providers>
  28.         </siteMap>
  29.  
  30.  
  31.     </system.web>
  32. </configuration>
  33.  
Here are my sitemaps (admin):
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  3.     <siteMapNode url="" title=""  description="">
  4.  
  5.       <siteMapNode url="admin\ChangePassword.aspx" title="Change Password"  description="Change User Name or Password" />
  6.       <siteMapNode url="admin\NewRunner.aspx" title="New Runner"  description="Add New Runners" roles="Administrator"/>
  7.       <siteMapNode url="admin\UpdateRunner.aspx" title="Update Runner"  description="Update Runner information" roles="Administrator"/>
  8.       <siteMapNode url="admin\RaceTimes.aspx" title="Enter Race Times"  description="Enter Race Times" roles="Administrator"/>
  9.       <siteMapNode url="admin\EditTimes.aspx" title="Edit Race Times"  description="Edit Current Year Race Times" roles="Administrator"/>
  10.       <siteMapNode url="admin\RegisterRunner.aspx" title="Register Runners"  description="Register Runners for the year" roles="Administrator"/>
  11.       <siteMapNode url="admin\RegisteredRunners.aspx" title="List of Registered Runners"  description="List of Registered Runners" roles="Administrator"/>
  12.  
  13.  
  14.     </siteMapNode>
  15. </siteMap>
  16.  
NON ADMIN:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  3.     <siteMapNode url="" title=""  description="">
  4.  
  5.       <siteMapNode url="age.aspx" title=" Race Times by Sex/Age"  description="Report of Race times sorted first by Sex, then Age groups" />
  6.       <siteMapNode url="individual.aspx" title="Individual Race Times"  description=" Race Times for each individual runner" />
  7.       <siteMapNode url="Overall.aspx" title="All Marathon Totals"  description="Report of overall Marathon Progress" />
  8.       <siteMapNode url="OverallbyAge.aspx" title="Marathon Totals by Sex/Age"  description="Report of overall Marathon Progress by Sex/Age" />
  9.       <siteMapNode url="http://www.pfrpc.com"  title="PFPRC Home"  description="Park Forest Running and Pancake Club Home Page" target="_blank"/>
  10.       <siteMapNode url="http://www.pfrpc.com/about-us.html"  title="About PFPRC"  description="About the Park Forest Running and Pancake Club" target="_blank"/>
  11.       <siteMapNode url="http://www.pfrpc.com/contact-us.html"  title="Contact Us"  description="Contact the PFRPC" target="_blank"/>
  12.  
  13.  
  14.     </siteMapNode>
  15. </siteMap>
  16.  
HERE IS MY MASTERPAGE:
Expand|Select|Wrap|Line Numbers
  1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="test" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <!-- Header -->
  12.     <div class="Header">
  13.         <asp:Image ID="Image1" runat="server" ImageUrl="~/images/title.png" AlternateText="PFRPC Progressive Marathon" Width="750px" />&nbsp;<br />
  14.  
  15.     </div>
  16.  
  17.     <!--Menu-->
  18.    <div class="Menu">
  19.    <asp:SiteMapDataSource id="NotLoggedInSiteMap" runat="server" ShowStartingNode="false" SiteMapProvider="MyXmlSiteMapProvider2"  />
  20.        <asp:HyperLink ID="hlHome" runat="server" NavigateUrl="~/Default.aspx">Home</asp:HyperLink><br />
  21.        <asp:HyperLink ID="hlLogin" runat="server" NavigateUrl="~/login.aspx">Admin Login</asp:HyperLink>
  22.        <asp:HyperLink ID="hlLogout" runat="server" NavigateUrl="~admin/LogoutSuccess.aspx" Visible="False">Logout</asp:HyperLink>
  23.    <asp:Menu ID="NotLoggedInMenu" runat="server" DataSourceID="NotLoggedInSiteMap" OnMenuItemDataBound="NotLoggedInMenu_MenuItemDataBound">
  24.  
  25.         </asp:Menu>
  26.    </div>     
  27.  
  28.    <!-- Menu -->
  29.    <div class="Menu2">
  30.    <asp:SiteMapDataSource id="LoggedInSiteMap" runat="server" ShowStartingNode="false" SiteMapProvider="MyXmlSiteMapProvider"  />
  31.    <asp:Menu id="LoggedInMenu" runat="server"  DataSourceID="LoggedInSiteMap" Visible="true" ></asp:Menu>
  32.        &nbsp;&nbsp;
  33.  
  34.    </div>
  35.  
MASTERPAGE CODE BEHIND:
Expand|Select|Wrap|Line Numbers
  1. public partial class test : System.Web.UI.MasterPage
  2. {
  3.     protected void Page_Load(object sender, EventArgs e)
  4.     {
  5.  
  6.     }
  7.  
  8.     protected void NotLoggedInMenu_MenuItemDataBound(object sender, MenuEventArgs e)
  9.     {
  10.  
  11.         e.Item.Target = ((SiteMapNode)(e.Item.DataItem))["target"];
  12.     }
Aug 11 '07 #13
Never mind. I think I got it working. The next step is to upload it to the server and see if it works!
Aug 11 '07 #14
Well I uploaded it to the server and it isn't working. This is quite frustrating!

When I go to log in I just get an application runtime error. Any thoughts on why??

I am going to email them back about this.

This was the web hosts answer:
Dear Customer,

Thank you for your inquiry regarding your account(s).

Free sites are hosted in busy environment. They are
hosted in Application pools where many others sites
are hosted too. The application pools have a limit on
amount of memory they can use. After reaching the
memory limit application pool will recycle causing a
reset of session variables among other things.
If sites that have a memory leaks are hosted with your
site in the same application pool you'll experience a
problem you've described.
Without complicated investigation we cannot detect a
sites with severe memory leaks. An we do not
investigate this sort of problems on our servers with
free hosting plans.
There are however many web servers with hundreds of
application pools where you can host you site.

To workaround the issue: Delete your site wait 5
minutes, and then register it again.
-----------------------------------------------

I guess I can try that but after getting form validation to work I see how much nicer and secure it is. I don't want to go back...

Any thoughts or suggestions would be much appreciated!
Aug 12 '07 #15
Never mind, I think I figured it out. Since using the form authentication creates a sql database I think that is my problem. My free webhost doesn't support sql databases, only access databases.. Fun...

Thanks for the help!
Aug 14 '07 #16
Frinavale
9,735 Expert Mod 8TB
Never mind, I think I figured it out. Since using the form authentication creates a sql database I think that is my problem. My free webhost doesn't support sql databases, only access databases.. Fun...

Thanks for the help!
That's to bad about the database.
You can customize your Forms Authentication to not use a database by creating a custom Membership object.

Or

More simply you could use the web.config file to store your user information instead of a database!
Aug 14 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Larry Woods | last post by:
I have a site that works fine for days, then suddenly, I start getting ASP 0115 errors with an indication that session variables IN SEPARATE SESSIONS have disappeared! First, for background...
6
by: Al Jones | last post by:
This is a repost form the vbscript newgroup - if this isn't the appropriate group would you point me toward one that is. Basically, I seem to be losing session data part way though preparing an...
2
by: Paul H | last post by:
Hi there I'm having a problem where I'm losing Session variables as soon as they've been set up. The user logs onto the system, the system sets some Session variables, and redirects to a menu...
3
by: bennett | last post by:
In the web.config file for my application, in the <sessionState> section I have set timeout="120" (in minutes), but session state variables in my application seem to be expiring in about 5 minutes....
10
by: tshad | last post by:
I have been using the default session state (InProc) and have found that I have been loosing my information after a period of time (normally 20 minutes). Is there anyway to find out how much...
3
by: Phillip N Rounds | last post by:
I'm writing a user control which has two states: Active & InActive. I additionally am required that there to be only one active control per page, and all logic has to be contained within the...
4
by: Not Me | last post by:
Hi, I have a set of pages that use forms authentication for access. There are times when one of the session objects I'm using appears to disappear. The session is not timing out, however. (if...
12
by: MrHelpMe | last post by:
Hello again all, I've finished my whole application and now I don't like the whole session variables that I am using. I have a form, user fills in info clicks submit and using CDOSYSMail an...
4
by: Nathan Sokalski | last post by:
I have a page that uses Session variables when generating the SQL statements used to submit and retrieve data from a database. However, because I don't know how long the user will be on the page,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.