Search code examples
c#asp.net-coreasp.net-identity

Setting a user detail in Login.cshtml.cs


Edit for clarity:

user.LastActiveDate in Login.cshtml.cs is successfully getting the data from the user (Screenshot showing this), but I am unable to actually change the data of LastActiveDate to current date.

Original Question:

I have set up AspNetCore.Identity.UI and have set up Login and Registration through it. As soon as the user logs in, I am trying to set up a detail LastActiveDate, which would contain DateTime.UtcNow, which should be set as soon as the user logs in.

What I've tried is setting this in the if(result.Succeeded), however this doesn't seem to be working. Any idea on what I might be doing wrong/idea on how I should be doing this?

Login.cshtml.cs

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");

            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
        
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
                if (result.Succeeded)
                {
                    //THIS NEEDS TO SET LastActiveDate IN User TO DateTime.UtcNow
                    var user = await _userManager.FindByEmailAsync(Input.Email);
                    user.LastActiveDate = DateTime.UtcNow;

                    _logger.LogInformation("User logged in.");
                    return LocalRedirect(returnUrl);
                }
                ...
            }
            ...
      }
      ...

User.cs

namespace Nemesys.Models
{
    public class User : IdentityUser
    {
       public DateTime LastActiveDate { get; set; }
       ...
    }
}

Just in case, this is how I set up the new user during Registration in Register.cshtml.cs, but I highly doubt there is any issue with this as it seems to be displaying the set DateTime.UtcNow just fine.

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new User {
                    ...
                    LastActiveDate = DateTime.UtcNow,
                };
...

I am quite sure there is a quick way to do this but I'm quite new to C# and can't figure it out or find a related query on it online. Any help would be highly appreciated.


Solution

  • No. You must set user detail by write it to AspNetUsers table. You maybe need extend user entity (it is a popular practice), see https://www.freecodespot.com/blog/custom-identity-columns-in-aspdotnet-core/

    For a specific case (in comment), record last log in time, you need extend AspNetUsers, by extend entity

    public class ApplicationUser : IdentityUser
    {
         public virtual DateTime? LastLoginTime { get; set; }
         public virtual DateTime? RegistrationDate { get; set; }
    
        // other properties
    }
    

    See more at https://stackoverflow.com/a/24618153/3728901