9

I am building a Asp.net Application. I need to save a HashTable in a session.

At page load i am writing

 protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       Session["AttemptCount"]=new Hashtable(); //Because of this line.
    }   
}

Here problem is, when a user refresh the page, session["AttemptCount"] also get refreshed. I want to know where should I declare

Session["AttemptCount"]=new Hashtable();

So that my seesion do not get refeshed.

EDIT In Global.asax, this session will get started, as soon as user opens the website. I want to creat this session only if user go to a particular page. i.e Login.aspx

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163

5 Answers5

17

Do it in the Session_Start method in your Global.asax like so...

protected void Session_Start(object sender, EventArgs e)
{
    Session["AttemptCount"]=new Hashtable();
}

Update:

Then simply just do a check to see if the session variable exists, if it doesn't only then create the variable. You could stick it in a property to make things cleaner like so...

public Hashtable AttemptCount
{
    get 
    {
        if (Session["AttemptCount"] == null)
            Session["AttemptCount"]=new Hashtable();
        return Session["AttemptCount"];
    }
}

And then you could just call on the property AttemptCount wherever you need like so...

public void doEvent(object sender, EventArgs e)
{
    AttemptCount.Add("Key1", "Value1");
}
Naeem Sarfraz
  • 7,360
  • 5
  • 37
  • 63
  • In Global.asax, this session will get started, as soon as user opens the website. I want to creat this session only if user go to a particular page. i.e Login.aspx – Vaibhav Jain Feb 26 '10 at 20:42
  • I've updated my answer, you'll simply need to check to see if it exists by checking against null – Naeem Sarfraz Feb 26 '10 at 20:53
  • What do you store in the hashtable? Is it the number of times the user has attempted to login? – Naeem Sarfraz Feb 26 '10 at 20:57
  • Yes, I am storing no. of attempts to login for a particular user. So if a particular user attempt to login, more than 15 times, I can block that session. – Vaibhav Jain Mar 01 '10 at 13:37
  • 3
    This will be very easy for people to get around by disabling session cookies. – Martin Smith Mar 07 '10 at 14:48
  • I record log in failures in a database table with the date and time. If 5 failed log in attempts occur from the same IP address, I kick that IP address out for 15 minutes. Not foolproof, but anyone attempting to constantly log in will have to change their IP address to continue after 5 failed attempts within 15 minutes. – Martin Smellworse Jan 21 '14 at 13:56
3

You could make a property like this in your page:

protected Hashtable AttemptCount
{
  get
  {
    if (Session["AttemptCount"] == null)
      Session["AttemptCount"] = new Hashtable();
    return Session["AttemptCount"] as Hashtable; 
  }
}

then you can use it without having to worry:

protected void Page_Load(object sender, EventArgs e)
{
  this.AttemptCount.Add("key", "value");
}
M4N
  • 94,805
  • 45
  • 217
  • 260
2

test if it exists first

 protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       if(Session["AttemptCount"] == null)
       {
          Session["AttemptCount"]=new Hashtable(); //Because of this line.
       }
    }   
}

though the session_start is better, you only need to uses it on one page but you can create it for each session.

Pharabus
  • 6,081
  • 1
  • 26
  • 39
1
Hashtable hastable_name=new Hashtable()
Session["AttemptCount"]=hastable_name
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
0

Look at Global.asax and the Application_Started (I think) and there is one for session started too.

Ryan O'Neill
  • 5,410
  • 4
  • 46
  • 69