Properties become inexplicably null

Viewed 19

I'm building a RBAC .NET Application with Identity. Actually there are 6 Roles already stored. I'm trying to add one of these roles to the current signed in user. The error raise exactly when i try to click the add user to role button. The specific button is the following :

<a asp-page-handler="AddUserToRole" asp-route-id="@user.Id" class="btn btn-success">Add</a>

This is the property that represents the current role in the page es. "Dev, Admin, Manager, Helper etc."

public IdentityRole role { get; set; } = new IdentityRole();

When i go to the page, the role property is correctly populated with all the properties(ID, name, normalizedName) through the OnGetAsync handler. And it's ok.

public async Task<IActionResult> OnGetAsync(string id)
        {

            // Find the role through id
            IdentityRole roleFound = await RoleManager.FindByIdAsync(id);

            // The population happens here(and it's ok)
            role = roleFound;

            // Get all users
            IQueryable<IdentityUser> usersMembers = UserManager.Users;

            // Loop through users to find who has the role and who doesn't have the role
            foreach (IdentityUser user in usersMembers)
            {
                bool? result = await UserManager.IsInRoleAsync(user, role.Name);

                if (result != null)
                {
                    if (result == true)
                    {
                        members.Add(user); // User
                    }
                    else
                    {
                        nonMembers.Add(user); // No user
                    }
                }
            }

            return Page();
        }

AddUserToRole(where the error occurs)

The error says that role properties "name" and "normalizedName" are null, except the ID. So the roleName property that I need in "OnGetAddUserToRole()" is null. And I don't understand why this property becomes null. **

public async Task<IActionResult> OnGetAddUserToRoleAsync(string id)
        {

            IdentityUser user = await UserManager.FindByIdAsync(id);
            
            // The error appears exactly here cause of "role.NormalizedName" is inexplicably null.
            IdentityResult res = await UserManager.AddToRoleAsync(user, role.NormalizedName);

            if (res.Succeeded)
            {
                Console.WriteLine($"role {role.Name} is added with success to {user.UserName}");
                return Redirect("/Account");
            }
            else
            {
                Console.WriteLine("error occured in 'AddUserToRole()'");
                return Page();
            }
        }

This is the Razor page :

<table class="table table-dark table-hover">
        <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">ID</th>
                <th scope="col">Email</th>
                <th scope="col">Name</th>
                <th scope="col">Add</th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.nonMembers.Count(); i++)
            {
                IdentityUser user = Model.nonMembers[i];
                <tr>
                    <th scope="row">@(i + 1)</th>
                    <td>@user.Id</td>
                    <td>@user.Email</td>
                    <td>@user.UserName</td>
                    <td>
                        <a asp-page-handler="AddUserToRole" asp-route-id="@user.Id" class="btn btn-success">Add</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>
0 Answers
Related