How can I trigger an auto-logout within a Windows Forms Application?

前端 未结 4 1000
花落未央
花落未央 2020-12-10 09:24

I have a Windows App Project to which users can login with their userid and passwords. I want to make it so that when a user logs in, I will get the Login Time, and if the u

4条回答
  •  借酒劲吻你
    2020-12-10 09:56

    1.st: User logs in, store the timestamp somewhere (for example this unix timestamp '1294230230' says it's aproximately 5th January 2011, 12:24)
    int sess_creation_time = now(); *lets say 'now()' function returns current unix timestamp

    2.nd: when user tries to perform any action, catch the timestamp of this attempt.
    int temp_time = now();
    Now, simply compare these values with your desired auto logout limit.

    // compare here
    // a, temp_time - sess_creation_time => the difference, time of inactivity
    // 60*30 -> 60 seconds * 30 -> 30 minutes
    if( (temp_time - sess_creation_time) > (60*30) )
    {
      // time of inactivity is higher than allowed, logout here
      logout();
    }
    else
    {
      // session is still valid, do not forget to update its creation time
      sess_creation_time = now();
    }
    

    Do not forget, this is not written in C/C++ or C#, but the logic remains the same ;-)
    Hope, this helps you a bit

提交回复
热议问题