Search code examples
asp.net-coreidentityasp.net-core-identity

how to get Username by id in identity?


I'm trying to get the user name by id and pass it to view by viewbag. I can access the user by id but I can't access the user name. I'm using identity 3 in asp.net core. This is my action:

 public IActionResult Index(string id)
    {
        var userCourse = _courseService.GetUserCourse(id);
        var user = _userManager.FindByIdAsync(id);
        return View(userCourse);
    }

Solution

  • public async Task<IActionResult> Index(string id)
    {
        var userCourse = _courseService.GetUserCourse(id);
        var user = await _userManager.FindByIdAsync(id);
        ViewBag.UserName= user.UserName;
        return View(userCourse);
    }
    

    And in View use viewbag.UserName to render userName

    <label>User Name:</label>  @ViewBag.UserName