0

I have the following code from the site master (where the login form is)

protected void btnLogin_Click(object sender, EventArgs e)
    {
        string dt = DateTime.Now.ToString();
        Response.Cookies["LastLogin"].Value = dt.ToString();
        Response.Cookies["LastLogin"].Expires = DateTime.Now.AddDays(365);
    }

So basically I save the current datetime in a cookie when a users logs in.

Then in the profile review page, I wrote the following:

protected void Page_Load(object sender, EventArgs e) {
  if (!Page.IsPostBack)
        {
            if (Request.Cookies["LastLogin"] != null)
                {
                    lblMessage.Text = Request.Cookies["LastLogin"].Value;
                }
         }}

The thing is that it works (it displays the date and time), but not for the previous log in but for the actual login, obviously, but thats why I'm asking - How can I solve this without having to do anything with database? How can I not "override" the previous value but also save the new one?

bashbin
  • 415
  • 1
  • 7
  • 21
  • Maybe I should create a string array and then when I Request the cookie, request the i-1 value? I don't know if that is possible though, I'm new with cookie codes – bashbin May 03 '17 at 21:47
  • take a look at my answer and let me know if it works for you? Thanks! – degant May 04 '17 at 08:06
  • @degant Sorry for late response! Yes it works, thanks a lot – bashbin May 06 '17 at 00:42

2 Answers2

2

Using one cookie LastLogin to save the last login information which can used by rest of your application and CurrentLogin to save current login timestamp.

protected void btnLogin_Click(object sender, EventArgs e)
{
    string dt = DateTime.Now.ToString();
    if (Response.Cookies["CurrentLogin"] != null)
    {
        HttpCookie oldLoginCookie = new HttpCookie("LastLogin")
        {
            Expires = Response.Cookies["CurrentLogin"].Expires,
            Value = Response.Cookies["CurrentLogin"].Value
        };
        Response.SetCookie(oldLoginCookie);
    }
    HttpCookie loginCookie = new HttpCookie("CurrentLogin")
    {
        Expires = DateTime.Now.AddDays(365),
        Value = dt.ToString()
    };
    Response.Cookies.Add(loginCookie);
}

Also using SetCookie() instead of Cookies.Add() to avoid multiple cookies from being added as advised here. Your Page_Load methods should work as-is as long as you make the above changes.

Community
  • 1
  • 1
degant
  • 4,861
  • 1
  • 17
  • 29
0

When doing the login, you can store the previous date (Response.Cookies["LastLogin"].Value, before overwriting it) into a new cookie - then check this new cookie in your page load.

You do need to check that the LastLogin is not empty, in case this is the very first login.

Something like this:

protected void btnLogin_Click(object sender, EventArgs e)
{
    if(Request.Cookies["LastLogin"] != null)
    {
         Request.Cookies["PrevLogin"].Value = Request.Cookies["LastLogin"].Value;
         Request.Cookies["PrevLogin"].Expires = DateTime.Now.AddDays(365);
    }

    string dt = DateTime.Now.ToString();
    Response.Cookies["LastLogin"].Value = dt.ToString();
    Response.Cookies["LastLogin"].Expires = DateTime.Now.AddDays(365);
}
DigiFriend
  • 1,164
  • 5
  • 10
  • Hmm I'm getting an error at line Request.Cookies["PrevLogin"].Value = Request.Cookies["LastLogin"].Value; as Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. – bashbin May 03 '17 at 22:14