locked
[MVC] UserManager fails to retreive a known User ? RRS feed

  • Question

  • User-1370514677 posted

    Hi everyone,

    I'm having an issue while trying to retreive users with their Id (which is a string in Identity) :

    var current_user = await _userManager.FindByIdAsync(UserId); // UserId (e8da5c53-34b1-4dcf-98b2-0cc978a35117) is a string passed to a Controller Method
    
    System.Console.WriteLine("NAME = " + current_user.UserName); // Will cause a NullReference Exception Object not set to an instance

    I don't understand this error as the Id e8da5c53-34b1-4dcf-98b2-0cc978a35117 is indeed present in my UserList.

    Best Regards

    Friday, February 26, 2021 4:06 PM

Answers

  • User475983607 posted

    Unless you changed the default route parameter configuration, the route name is "id" not "UserId".   The debugger clearly shows the UserId string is null if you are using the defaults.   Also the AdminViewModel parameter does not make sense for an HTTP GET.  This approach is not found in any tutorial.  Can you explain how or why you came up with this design approach?

    The ChangeUserRole should have the following signature if you are using the default route parameter configuration.

    [HttpGet]
    public async Task<IActionResult> ChangeUserRole(string id)

    In you past threads I've recommended  the Getting Started tutorials to learn best practices and common MVC programming patterns.   Your coding patterns indicate that you have not actually made and effort to go through these tutorials.  Maybe you looked at the tutorials but did not did not create the project(s)?  Perhaps you should set aside time to learn the fundamentals.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 26, 2021 9:01 PM

All replies

  • User475983607 posted

    We know FindByIdAsync works as expected.  My best guess is the User Id you are passing to FindByIdAsync () does not exist in database.  As recommended in your other threads, set a break point and debug your code.  Check the value of current_user.   Open the database table and look for (or query) for the GUID.  Does it exist?

    I don't understand this error as the Id e8da5c53-34b1-4dcf-98b2-0cc978a35117 is indeed present in my UserList.

    What is a UserList???

    You have to understand that we cannot reproduce this issue as we do not have your database and you did not include the UserList.  You really should learn how to troubleshoot and debug to find where your code stops working as expected.

    Friday, February 26, 2021 4:22 PM
  • User-1370514677 posted

    Hi @mgebhard,

    What is a UserList

    It's what is injected as a ViewModel :

            [HttpGet]
            public IActionResult Users()
            {
                var UserList = _userContext.User.ToList();
                
                return View(UserList);
            }

    I then loop through each Users present in the UserList :

    <table id="admin_table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Role</th>
                <th>Pseudo</th>
                <th>Email</th>
                <th>Actions</th>
            </tr>
        </thead>
    
        <tbody>
            @foreach(var user in Model)
            {
                <tr>
                    <td>@user.Id</td>
                    <td>@user.Role.Name</td>
                    <td>@user.UserName</td>
                    <td>@user.Email</td>
                    <td id="actions">
                        <a asp-area="Admin" asp-controller="Role" asp-action="ChangeUserRole" asp-route-id="@user.Id" class="action_item">Changer Rôle</a>
                        <a asp-area="Admin" asp-controller="User" asp-action="SendToUser" asp-route-id="@user.Id" class="action_item">Message</a>
                        <a asp-area="Admin" asp-controller="User" asp-action="DeleteUser" asp-route-id="@user.Id" class="action_item">Supprimer</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>

    And then if I click on "Changer Rôle" in order to change a user Role I get the NullReferenceException inside this method (UserId is passed to the method through asp-route-id), so these Ids do exist because they are shown :

    http://localhost:5000/Admin/Role/ChangeUserRole/eb750521-7037-44be-aed3-b9515e8a3279
            [HttpGet]
            public async Task<IActionResult> ChangeUserRole(AdminViewModel AdminViewModel, string UserId)
            {
                var current_user = await _userManager.FindByIdAsync(UserId);
    
                System.Console.WriteLine("NAME = " + current_user.UserName); // NullReference Exception
                System.Console.WriteLine("ROLE = " + current_user.Role.Name); // NullReference Exception
    
                return View(AdminViewModel);
            }

    Friday, February 26, 2021 6:57 PM
  • User475983607 posted

    Unless you changed the default route parameter configuration, the route name is "id" not "UserId".   The debugger clearly shows the UserId string is null if you are using the defaults.   Also the AdminViewModel parameter does not make sense for an HTTP GET.  This approach is not found in any tutorial.  Can you explain how or why you came up with this design approach?

    The ChangeUserRole should have the following signature if you are using the default route parameter configuration.

    [HttpGet]
    public async Task<IActionResult> ChangeUserRole(string id)

    In you past threads I've recommended  the Getting Started tutorials to learn best practices and common MVC programming patterns.   Your coding patterns indicate that you have not actually made and effort to go through these tutorials.  Maybe you looked at the tutorials but did not did not create the project(s)?  Perhaps you should set aside time to learn the fundamentals.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 26, 2021 9:01 PM
  • User-1370514677 posted

    Hi @mgebhard,

    Thanks a lot, I completely wrote my code without even thinking about the Id naming, it is indeed @user.Id so i've changed for a string Id parameter.

    And also I've read the tutorials and understood them, it's just that I completely mixed my thinking and wrote non sense ahah.

    You are absolutely right, an HttpGet shoudn't work like that.

    Here is the updated code, fully functional and correct :

    [HttpGet]
    public async Task<IActionResult> ChangeUserRole(string Id)
    {
        var CurrentUser = await _userManager.FindByIdAsync(Id);
    
        System.Console.WriteLine("NAME = " + CurrentUser.UserName);
        System.Console.WriteLine("ROLE = " + CurrentUser.Role.Name);
    
        return View(CurrentUser);
    }

    Friday, February 26, 2021 10:38 PM