user2138814
user2138814

Reputation: 77

How to find if the user has logged in for the first time - C#

The scenario is, If the user has logged in for the first using his one time password it should get redirected to ResetPassword.aspx

If the user is not new, then should get redirected to Main.aspx page.

Should I use IsPostBack or Membership.ValidateUser?

How do we usually code in C# to check if its a new user (using first time login)?

I am a newbie to programming not getting enough info on net. Please help

Upvotes: 1

Views: 3610

Answers (2)

Manish Mishra
Manish Mishra

Reputation: 12375

IsPostBack check won't help you with what you want. You need to maintain it through the database.

IsPostBack Gets a value that indicates whether the page is being rendered 
for the first time or is being loaded in response to a postback.

it has to do with the page post and not with your database or your users or your logic :)

To aid you with your logic, you need to maintain a separate column to identify whether the user has come for the first time or not.

one simple logic:

make a column in your table like, LastLoginDate make it nullable. When a user is registered, keep this field NULL.

When user logs in, simply place a check whether the LastLoginDate is NULL or not i.e.

   if(userObj.LastLoginDate == null)
   {
        //user has come for the first time
        //code to update the LastLoginDate to DateTime.Now
        Redirect("resetPassword.aspx");
   } 
   else 
   {
       //code to update the LastLoginDate to DateTime.Now
       Redirect("home.aspx");
   }

Upvotes: 4

Freelancer
Freelancer

Reputation: 9074

Membership.ValidateUser check if he/she is not approved or if provided with wrong credentials.

For this issue, set some flag in database when creating a new user.

When user loggs in, check its flag.

If it is indicating for new user then redirect it to resetpassword page.

And simply change the flag status after redirecting, so that for next time it will not indicate him as new user.

Upvotes: 1

Related Questions