How to get logged user data in ASP.NET Core 5 MVC (.NET 5)?

charitht99 perera

I'm currently working on an ASP.NET Core 5 MVC web project, I used default created Single User Account generated template for user management. I have applied the migration and it's working fine as expected. I need to get logged user, in .NET 5 Single User Account template AccountController was not visible. I tried following code outside the controller, it's giving null.

public class UserService  
{
    private readonly IHttpContextAccessor _context;

    public UserService(IHttpContextAccessor context)
    {
        _context = context;
    }

    public string GetUser()
    {
       return _context.HttpContext.User?.Identity?.Name;
    }
}
Do Nhu Vy

You can see sample code at here https://stackoverflow.com/a/38751842/3728901 (maybe, ASP.NET Core 1, 2, or 3)

Focus at

public class MyController : Microsoft.AspNetCore.Mvc.Controller
var id = _userManager.GetUserId(User); // Get user id:

or other way

private UserManager<ApplicationUser> _userManager;

//class constructor
public MyController(UserManager<ApplicationUser> userManager)
{
    _userManager = userManager;
}

and

var user = await _userManager.GetUserAsync(User);
var email = user.Email;

Because this question related .NET 5 therefore I don't consider the question is duplicated. Let's me know or revise small details if has minor changes in .NET 5.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get Logged in User Role: Asp.net MVC5

Get UserID of logged-in user in Asp.Net MVC 5

How to get the currently logged in user name through an interface to insert in updated by column in asp.net core mvc?

How to get items for logged in user ASP.NET MVC

How to get the current logged in user Id in ASP.NET Core

ASP.NET MVC: Get user id of currently logged in user

CustomAuthorizeAttribute redirects to LoginView even when the user is logged in (ASP.Net MVC 5)

Get role/s of current logged in user in ASP.NET Core MVC

How to logout user in OWIN ASP.NET MVC5

How to send data from view to controller with drop down list asp.net core 5 MVC

How to check if a logged in user is in a role in ASP.NET Core

How to get logged-in user name in the constructor of a controller in ASP.NET Core Identity

How to get the current logged User in an ASP.NET Core WebApi Controller

How to get data from a html5 web form on a controller asp.net mvc?

ASP.NET MVC 5 get claims

Check if user is logged on in ASP.NET Core

Localization and Globalization in ASP.NET Core MVC (.NET 5)

How to get logged in user information in ASP.NET

What is the difference between ASP.NET MVC 5, ASP.NET Core MVC 5?

ASP.NET MVC5 Display data to user with specific selected data"Branch"

Friendly URLs in ASP.NET MVC 6 (ASP.NET Core or ASP.NET5)

How To Restrict User Entries Only to that Sepcific User in EF 6/ASP.NET MVC 5

Get ID from item which is checked from checkbox in ASP.NET Core 5 MVC

Allow user to see only their uploaded data (Asp.NET MVC5)

Create additional data when user get registered (ASP.NET Core MVC 2.1)

Asp net mvc 5 Security

ASP NET MVC 5 SessionState

ASP.NET Core 5 MVC Calculate Salary with Employee Rank

Redirect to page OnActionExecuting method ASP.NET Core 5 MVC