Click here to Skip to main content
15,396,498 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to redirect user to particular page after identity login based on roles. I am using identity login.

Below is the code which i am trying. But this is not redirecting me anywhere. If i remove the conditions of roles then it is redirecting.

C#
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
    {
        returnUrl = returnUrl ?? Url.Content("~/");

        if (ModelState.IsValid)
        {
            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, set lockoutOnFailure: true
            var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);

            if (result.Succeeded)
            {
                if(!string.IsNullOrEmpty(returnUrl))
                {
                    _logger.LogInformation("User logged in.");
                    if (User.IsInRole("Student"))
                    {
                        return Redirect("~/Student/Dashboard");
                    }
                    else if (User.IsInRole("Counsellor"))
                    {
                        return Redirect("~/Counsellor/Dashboard");
                    }
                    else if (User.IsInRole("School"))
                    {
                        return Redirect("~/School/Dashboard");
                    }
                    else if (User.IsInRole("Admin"))
                    {
                        return Redirect("~/Admin/Dashboard");
                    }


                }
                else

                {
                    _logger.LogInformation("User logged in.");
                    return LocalRedirect(returnUrl);

                }

               // return LocalRedirect(returnUrl);
            }
            if (result.RequiresTwoFactor)
            {
                return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
            }
            if (result.IsLockedOut)
            {
                _logger.LogWarning("User account locked out.");
                return RedirectToPage("./Lockout");
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                return Page();
            }
        }

        // If we got this far, something failed, redisplay form
        return Page();
    }


What I have tried:

if i do it like following then it is redirecting:

<pre lang="c#">if(!string.IsNullOrEmpty(returnUrl))
            {
                _logger.LogInformation("User logged in.");

                    return Redirect("~/Student/Dashboard");

            }


whats the reason for this and how can i resolve this?
Posted
Updated 2-Sep-20 8:26am
Comments
F-ES Sitecore 12-Jun-20 8:38am
   
We don't have access to your accounts or credentials, we don't know your roles or if things are properly configured so it is hard to say. What I do advise you do is to use the debugger to step through your code line by line to see what path it is taking and how that path differs from what you exect. eg if your IsInRole is returning false when you think it should be true then you need to focus on why that is happening.
Harpreet_125 12-Jun-20 8:43am
   
It is going to the right path. Debugger is returning false value for the following code.. that's the problem.. but i dont see anything wrong..

if (User.IsInRole("Student"))
{
return Redirect("~/Student/Dashboard");
}
MT_2022 12-Mar-22 20:39pm
   
The User context is null at this point of the sign in process which is why the if condition is returning false.

1 solution

//Your returnUrl value always is null.

public async Task<IActionResult> OnPostAsync(string returnUrl = null)//here you are setting null to returnUrl
    {
        returnUrl = returnUrl ?? Url.Content("~/"); //redirect to this url ("~/") if is null

        if (ModelState.IsValid)
        {
            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, set lockoutOnFailure: true
            var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);

            if (result.Succeeded)
            {
                //if(!string.IsNullOrEmpty(returnUrl))//returnUrl = null, so will not enter to the if block, remove that block
                //{
                  //  _logger.LogInformation("User logged in.");
                    if (User.IsInRole("Student"))
                    {
                        return Redirect("~/Student/Dashboard");
                    }
                    else if (User.IsInRole("Counsellor"))
                    {
                        return Redirect("~/Counsellor/Dashboard");
                    }
                    else if (User.IsInRole("School"))
                    {
                        return Redirect("~/School/Dashboard");
                    }
                    else if (User.IsInRole("Admin"))
                    {
                        return Redirect("~/Admin/Dashboard");
                    }


                //}
               // else

                //{
					else{
                    _logger.LogInformation("User logged in.");
                    return LocalRedirect(returnUrl);//redirect to this page returnUrl = ("~/")
					}
               // }

               
            }
            if (result.RequiresTwoFactor)
            {
                return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
            }
            if (result.IsLockedOut)
            {
                _logger.LogWarning("User account locked out.");
                return RedirectToPage("./Lockout");
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                return Page();
            }
        }

        // If we got this far, something failed, redisplay form
        return Page();
    }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900