1

Alright so I have a task, that I have to let the client try to enter the password 3 times, if he doesn't enter the right password in 3 times, it will redirect him to another page, The thing is that I don't know how can I use the session, how can I do like ++ or something.

Session["counter"] = 0;

And I am trying to do the following:

Session["counter"]++;

How can I detect if the client tried to enter the password 3 times? Thanks

4 Answers 4

2
int counter=1;
Session["counter"]=counter;

When you want to update that, read the value and convert it to int and then increase, save back

if(Session["counter"]!=null)
{
 counter=Convert.ToInt32(Session["counter"]);
}
counter++;
Session["counter"]=counter;

EDIT : As per the comment, This is how you can check the counter value. I wrapped the checking inside 2 methods to set and get, you can even use Properties as others mentioned.

private int GetLoginAttempts()
{
    int counter=0;
    if(Session["counter"]!=null)
    {
     counter=Convert.ToInt32(Session["counter"]);
    }
  return counter;
}
private void IncreaseLoginAttempts()
{
   if(Session["counter"]!=null)
   {
     counter=Convert.ToInt32(Session["counter"]);
   }
   counter++;
   Session["counter"]=counter;
}

and when user tries to login( in your button click / action method ), check the Current value

   if(GetLoginAttempts()==3)
   {
        //This means user already tried 3 times, show him a message !
   }
   else
   {
        //Do the login process, If login fails, increase the counter 
       IncreaseLoginAttempts()
   }
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer, but how can I check if the Session counter is 3 for example? how can I make that if statement?
Can we use ViewState here instead of Session ?
@PankajGarg: Yes we can. Since other people already wrote answer based on that, i skipped. and i am not sure OP is comfortable with Properties. so i wrote methods to make him understand better and simple way. In properties also you do the same thing. another thing is i am not sure whether OP has a class where he can have this property
Thank you very much for your very detailed answer, helped me alot, appreciated
@PankajGarg : I don't see anything wrong with that. I would like to separate my functionality to separate methods to make it more readable. there is no such rule exists. As long as your code is readable and modularized, it is fine
1

Try this.

int counter = Int32.Parse(Session["counter"].ToString()); //Session["counter"] may be null

Session["counter"] = ++counter;

Comments

0

You could implement it by wrapping it in a property as so:

    public int PasswordAttempts
    {
        get
        {
            if (Session["PasswordAttempts"] == null)
                Session["PasswordAttempts"] = 0;

            return (int)Session["PasswordAttempts"];
        }
        set
        {
            Session["PasswordAttempts"] = value;
        }
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        PasswordAttempts++;
    }

Comments

0
private Int16 Counter
{
    get
    {
        if (ViewState["Counter"] == null)
            return 0;
        else
            return Convert.ToInt16(ViewState["Counter"]);
    }
    set
    {
        Int16 counter = 0;
        if (ViewState["Counter"] == null)
            counter = 0;
        else
            counter = Convert.ToInt16(ViewState["Counter"]);
        ViewState["Counter"] = counter + 1 + value;
    }
}
if(counter == 3)
{
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.