locked
Failed to Get the Current Logged in user RRS feed

  • Question

  • User-1230161102 posted

    Hi
    After I hosted a site in IIS, I am trying to get the current logged user but I keep getting 

    IIS APPPOOL/XYZ

    The command I am using is

    System.Security.Principal.WindowsIdentity.GetCurrent().Name

    I am not getting the user id of the domain user although it is working fine when I run it from inside visual studio. 

    In IIS I enabled windows authentication and disable all the rest.

    Please advise

    Saturday, July 18, 2020 4:31 PM

Answers

  • User-474980206 posted

    You are getting the thread identity, which is the pool account. Use Context.User.Name to get the login user. Context can get injected if you need it in middleware.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 18, 2020 6:24 PM
  • User-2054057000 posted

    You can use the below code to get your username:

    private UserManager<AppUser> userManager;
    public AccountController(UserManager<AppUser> userMgr)
    {
        userManager = userMgr;
    }
     
    [Authorize]
    public async Task<IActionResult> Index()
    {
        AppUser user = await userManager.GetUserAsync(HttpContext.User);
        string message = "Hello " + user.UserName;
        return View();
    }

    More in at Showing Logged in User’s name

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 19, 2020 3:15 AM
  • User-1230161102 posted

    I got a quick way to do it 

    User.Identity.Name.ToString();

    Thank u

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 20, 2020 5:28 AM

All replies

  • User-474980206 posted

    You are getting the thread identity, which is the pool account. Use Context.User.Name to get the login user. Context can get injected if you need it in middleware.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 18, 2020 6:24 PM
  • User-2054057000 posted

    You can use the below code to get your username:

    private UserManager<AppUser> userManager;
    public AccountController(UserManager<AppUser> userMgr)
    {
        userManager = userMgr;
    }
     
    [Authorize]
    public async Task<IActionResult> Index()
    {
        AppUser user = await userManager.GetUserAsync(HttpContext.User);
        string message = "Hello " + user.UserName;
        return View();
    }

    More in at Showing Logged in User’s name

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 19, 2020 3:15 AM
  • User-1230161102 posted

    I got a quick way to do it 

    User.Identity.Name.ToString();

    Thank u

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 20, 2020 5:28 AM