How to save user last login date if he was logged in with external login provider using ASP .Net Identity?

Dmytro

I've extended AspNetUser class with LastLoginDate property to remember each time user makes login.

public sealed partial class User : IdentityUser<int, UserLogin, UserRole, UserClaim>, IUser<int>, IDatabaseEntity
{
    ...
    public DateTime? LastLoginDate { get; set; }   
}

Loging works fine each time user makes usual login using email and password as login credentials. In AccountController I have my login action:

[HttpPost]
public ActionResult Login(LoginModel model)
{
    ...
    var result = SignInManager.PasswordSignIn(model.Email, model.Password, model.RememberMe);

    if (result == SignInStatus.Success)
    {
        var user = UserManager.FindByEmail(email);
        UserManager.RememberLoginDate(user);  
        return Redirect(model.ReturnUrl);
    }

    ...
}

RememberLoginDate is just an extension methods which basically just sets current time to user LastLoginDate property:

public static IdentityResult RememberLoginDate(this ApplicationUserManager manager, User user)
{
     user.LastLoginDate = DateTime.UtcNow;;
     return manager.Update(user);
}

But when I do external login like below:

[AllowAnonymous]
public ActionResult ExternalLoginCallback(string returnUrl, string provider)
{
     var loginInfo = AuthenticationManager.GetExternalLoginInfo();
     if (loginInfo == null)
          return RedirectToAction("ExternalLoginFail");

     // Sign in the user with this external login provider if the user already has a login
     var result = SignInManager.ExternalSignIn(loginInfo);
     if (result == SignInStatus.Success)
          return RedirectToLocal(returnUrl);

     if (result == SignInStatus.Failure)
     {
          return View("ExternalLoginConfirmation" ...) 
     }

     ...
}

How can I log user login date in this case? I have no info about the user such as email or Id to get him with UserManager at this point. I've already tried: User.Identity.GetUserId<int>() to get user Id after SignInManager.ExternalSignIn(loginInfo) but it is not updated after login, it still is default(int) - 0. SignInStatus is just an enum with no additional info about the logged user and use ExternalLoginInfo.Email is also not an option because this email can be different from user's actual registered email.

Matt Lassam-Jones

You should be able to use UserManager.FindAsync(loginInfo.Login) to pull your user back via the provider and key - this is how the SignInManager itself works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to authorize user who logged in using external login in Asp.Net MVC

External Login without using identity asp.net core 2.0

Sitecore - How to get User ID if the user was logged in using external identity provider (Salesforce SSO)

How to change user login credentials in identity asp.net mvc?

Not able to get user group claims when using Azure AD as external login alongside Identity Core(ASP.NET Core 2.1)

How do I get the user details straight after login using Identity on ASP.NET Core?

How to build login system so that only last user logged in can save?

ASP.NET Identity record user registration and last logged on time

c# Core MVC user identity / how can I save last login (via cookie)

Tow types of user logged in the same login form - ASP.NET

How to work with Azure AD external login with ASP.NET Idenity in Identity Server4

ASP.NET Identity - Maintain login after updating user record

ASP.NET Identity user null after login

Overlap User Login in Two Projects with ASP.NET Identity

How to disable account if user login fails continuously in Asp.Net Core identity

Asp.net identity - How to maintain logged in user role?

How to create an Asp.Net Identity Policy that represents a Logged In user?

How to restrict user to display login page if already logged in using Reactjs

Django: redirect the user from the login page if he has already logged in

Redirect user from TabBar to login viewController if he's not logged in (Swift)

How to Upload File using Application Pool identity instead of Logged User Identity- ASP.NET MVC Impersonation

How to check if user is logged in to ASP.NET Core web application when using ASP.NET Core Web API to house identity

asp.net identity using Owin with Google login: skip registration

How To Make Identity Login Page As Startup Page And Redirect User to Dashboard If User Is Already Logged-in In Blazor Server

How to login using e-mail in ASP.NET Identity 2.0 alpha 1?

How to refresh CSRF token on login when using cookie authentication without identity in ASP .NET Core Web API

How to configure ASP.NET Identity with Facebook login - strict redirect

ASP.net Web Forms Application login using external DB

ASP Net Core - Mixing External Identity Provider with Individual User Accounts for Audit Tracking