locked
ASP.NET Core MVC 2 : Can't get roles in Login Controller RRS feed

  • Question

  • User-560223997 posted

    I created authetication databases, and I can autheticate and get roles in my Main Controller. I've just implemented users and roles.

    However, in my Login Controller (actually AccountController), when I try to redirect the user to a page according to his/her roles, I can't. It seems that Users.IsInRole("....") doesn't function in my Login Controller, while works perfectly im my Main Controller.

    My Login Action code:

                    AppUser appuser = await userManager.FindByEmailAsync(details.Email);
    
                    if (appuser != null)
                    {
                        await signInManager.SignOutAsync();
                        Microsoft.AspNetCore.Identity.SignInResult result 
                            = await signInManager.PasswordSignInAsync(appuser, details.Password, false, false);
                        if (result.Succeeded)
                        {
                            if (User.IsInRole("Admin"))
                                return RedirectToAction("EntryAdmin","Home");
    
                            return RedirectToAction("Index", "Home");
                        }
                    }
    

    So, User.IsInRole don't work here.

    Could anyone help me? 

    Monday, April 13, 2020 11:09 AM

Answers

  • User475983607 posted

    However, in my Login Controller (actually AccountController), when I try to redirect the user to a page according to his/her roles, I can't. It seems that Users.IsInRole("....") doesn't function in my Login Controller, while works perfectly im my Main Controller.

    Correct.  The login request is not authenticated - the authentication cookie does not exist in the login request.  You need to read the user's role from the database then redirect.  Or you can redirect to another page which will have the cookie and redirect from there.  Simply the way cookie work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 13, 2020 1:48 PM
  • User-560223997 posted

    Ok. Now, I've found the solution:

    1st Possibility: In the Login Action use:

    IList<string> roles = userManager.GetRolesAsync(appuser);

    So, use userManager instead of User or appuser… The roles appear as a list of strings, with its names.

    2nd possibility: as @mgebhard said, is to deal with the roles in other action (e.g. in the Main Controller), and query User.IsInRole .

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 13, 2020 4:18 PM

All replies

  • User475983607 posted

    However, in my Login Controller (actually AccountController), when I try to redirect the user to a page according to his/her roles, I can't. It seems that Users.IsInRole("....") doesn't function in my Login Controller, while works perfectly im my Main Controller.

    Correct.  The login request is not authenticated - the authentication cookie does not exist in the login request.  You need to read the user's role from the database then redirect.  Or you can redirect to another page which will have the cookie and redirect from there.  Simply the way cookie work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 13, 2020 1:48 PM
  • User-560223997 posted

    Ok. Now, I've found the solution:

    1st Possibility: In the Login Action use:

    IList<string> roles = userManager.GetRolesAsync(appuser);

    So, use userManager instead of User or appuser… The roles appear as a list of strings, with its names.

    2nd possibility: as @mgebhard said, is to deal with the roles in other action (e.g. in the Main Controller), and query User.IsInRole .

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 13, 2020 4:18 PM