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

fosbie

I'm using ASP.NET Core Identity to do the authentication on my website and I'm trying to log user id to the DB as soon as they login but I don't seem to be able to access them using the following code. The GetResult() returns null here but it works fine if it's on the next page, but just not in the OnPostAsync method where it needs to be. Input.UserIDOrLogonName does not contain the user id.

private readonly SignInManager<User> _signInManager;
private readonly UserManager<User> _userManager;

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
    returnUrl = returnUrl ?? Url.Content("~/");
    if (ModelState.IsValid)
    {
        var result = await _signInManager.PasswordSignInAsync(Input.UserIDOrLogonName, Input.Password, Input.RememberMe, lockoutOnFailure: true);
        if (result.Succeeded)
        {
            var userId = _userManager.GetUserAsync(HttpContext.User)
                         .GetAwaiter().GetResult().UserId;
            _audit.RecordAction(AuditType.LoginSuccess, userId);
            return LocalRedirect(returnUrl);
        }

Is there any other way to get to the instance of the User?

Edward

For HttpContext.User, it will work for sub-request after loging.

If you want to log UserId, try to find the user by email like

if (result.Succeeded)
{
    var user = await _userManager.FindByEmailAsync(Input.Email);
    var userId = user.Id;
    _logger.LogInformation("User logged in.");
    return LocalRedirect(returnUrl);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pass user detail after Login Is successful From identity asp.net core 3.1

ASP.NET Core Identity - get current user

ASP.Net Core Identity login status lost after deploy

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

ASP.Net Core how to get the user role in EF Core and Identity

External Login without using identity asp.net core 2.0

Asp Core Identity, How to get userId after login?

How do I get a new Access Token before its expiration after user login with chrome.identity.launchWebAuthFlow?

How to get user claims after signin through SignInManager in ASP .NET CORE Identity?

ASP.NET Identity user null after login

How do I seed user roles in Identity ASP.NET Core 2.1 (Identity Scaffolded)

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

How to get Asp.net Core Identity User in View

Using Asp.Net Core Identity in MVC, Authorize attribute is rebouncing to login page after succesfull login

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

Adding Redirection immediately after Login in ASP.Net Core 2.1 using Identity Core

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

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

ASP.Net Core - How do I wait for User Identity to be available immediately after signing in?

ASP.NET Identity - Maintain login after updating user record

How get current login user in ViewComponent Asp.Net Core

Can I follow a not logged in user using Asp.Net Core Identity?

How do I get AD user when using ASP.Net Identity

How do I get the user id from my database and display it using ASP.NET Core MVC

User.Identity.IsAuthenticated is false in a non-auth methods after successful login in asp.net core

How do I add authentication to an ASP.NET Core minimal API using Identity, but without using Azure?

How do I update user claims in Asp.net Core

How can I get user details by UID after login with React and Firestore?

How can I have multiple Identity user types when using ASP.NET Core Identity and Entity Framework Core?

TOP Ranking

HotTag

Archive