How to track first login of user in MVC3?

2019-07-27 20:24发布

问题:

I am working in ASP.NET MVC3. When a user complete registration, I want to notify user to change personal information only in first lo-gin. How can I do it?

回答1:

You can always create a new variable to check if this is the user's first login, but that variable would become unused after the first login.

I assume you have a DateTime variable for UserLastLoggedin or something along those lines? If that is null, then the user is logging in for the first time.



回答2:

There are several ways to accomplish that. You can utilize Profiles, or create a table in your database where you can store userId along with an indicator if he's logging in for the first time or not.

Let's say you want to use profiles : add this to you web.config

<profile>
  <providers>
    <clear />
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
  </providers>
  <properties>
    <add name="EverLoggedOn" defaultValue="False" type="System.Boolean" />
  </properties>
</profile>

If you're using default AccountController take a look at this code :

            if (Membership.ValidateUser(model.UserName, model.Password))
            {

                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

                if (Roles.IsUserInRole(model.UserName, "Administrator"))
                {
                    return RedirectToAction("Index", "ManageApp");
                }
                var userProfile = ProfileBase.Create(model.UserName);
                var FirstTime = userProfile.GetPropertyValue("EverLoggedOn");
// rest of the code is up to you :)


回答3:

If a user completes your registration process you need to store a value which indicates that he has not yet changed his personal information yet.

Something like

isNewUser = true;