locked
User.IsInRole(Role) returns false in login page only RRS feed

  • Question

  • User-1741097413 posted

    so, I have been using User.IsInRole(Role) in views and controllers and its working fine, but now I'm trying to use it in the login razor page (the default identity page) and it always returns false.

    here is the code:

            public async Task<IActionResult> OnPostAsync(string returnUrl = null)
            {
                var user = await _userManager.FindByNameAsync(Input.Email) ?? await _userManager.FindByEmailAsync(Input.Email);
                returnUrl = returnUrl ?? Url.Content("~/");
                //var userRole = await _userManager.GetRolesAsync(user);
                var userFromDb = await _db.ApplicationUser.FindAsync(user.Id);
    
                if (ModelState.IsValid)
                {
                    var result = await _signInManager.PasswordSignInAsync(user, Input.Password, Input.RememberMe, lockoutOnFailure: true);
                    if (result.Succeeded)
                    {
                        if (User.IsInRole(SD.AdminUser))
                        {
                            if (userFromDb.Status == SD.StatusApproved)
                            {
                                _logger.LogInformation("User logged in.");
                                return LocalRedirect(returnUrl);
    
                            } else if (userFromDb.Status == SD.StatusExpired)
                            {
                                _logger.LogWarning("User account expired.");
                                return RedirectToPage("./Expired");
                            } 
                        } else if (User.IsInRole(SD.Role2) || User.IsInRole(SD.Role3)) 
                        {
                            if (userFromDb.Status == SD.StatusApproved)
                            {
                                _logger.LogInformation("User logged in.");
                                return LocalRedirect(returnUrl);
                            } else
                            {
                                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                                await _signInManager.SignOutAsync();
                                return Page();
                            }
                        } else // it always comes here
                        {
                            _logger.LogInformation("User logged in.");
                            return LocalRedirect(returnUrl);
                        }
                    }
                }

    and of course I've included these in startup.cs

                services.AddIdentityCore<ApplicationUser>()
                    .AddRoles<IdentityRole>()
                    .AddClaimsPrincipalFactory<UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>>()
                    .AddEntityFrameworkStores<ApplicationDbContext>()

    Thank you.

    Saturday, October 3, 2020 12:54 AM

Answers

  • User-474980206 posted

    As login page user is anonymous. there are no roles loaded. User will be valid on the next request. You can just check the user.Roles

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 3, 2020 1:59 AM
  • User-2054057000 posted

    You need to update your action method by including the use of the function called IsInRoleAsync(). The function will check if the user is in a role.

    await userManager.IsInRoleAsync(user, "Admin");

    Here user is the one whose role membership is to be checked. 

    You can also refer this tutorial - How to work with Roles in ASP.NET Core Identity


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 3, 2020 12:58 PM

All replies

  • User-474980206 posted

    As login page user is anonymous. there are no roles loaded. User will be valid on the next request. You can just check the user.Roles

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 3, 2020 1:59 AM
  • User-2054057000 posted

    You need to update your action method by including the use of the function called IsInRoleAsync(). The function will check if the user is in a role.

    await userManager.IsInRoleAsync(user, "Admin");

    Here user is the one whose role membership is to be checked. 

    You can also refer this tutorial - How to work with Roles in ASP.NET Core Identity


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 3, 2020 12:58 PM