1

I'm trying to redirect users upon login to different pages, depending on their role.

Users with a local Identity account are redirected properly in the Login method, by using User.IsInRole("RoleName").

However, when I try to conditionally redirect users who are using external validation, it fails to find the role, because the User is not set until after the redirect:

        public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            return RedirectToAction("Login");
        }

        // Sign in the user with this external login provider if the user already has a login
        var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);

                if(User.IsInRole("Administrator")) 
//always evaluates to false because User is null
                {
                    returnUrl = "~/admin";
                } else
                {
                    returnUrl = "~/dashboard";
                }
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false });
            case SignInStatus.Failure:
            default:
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
        }
    }

It seems the User isn't completely logged in until after the RedirectToLocal() call triggers.

How can I check the external login user's roles before redirecting?

Beofett
  • 2,388
  • 1
  • 23
  • 37
  • check this Link for clues and or answers http://stackoverflow.com/questions/21470423/using-asp-net-identity-for-a-role-provider-easily – MethodMan Nov 13 '15 at 21:58

1 Answers1

2

You are right, at least one new call is needed to apply user authentication. But if you don't want to redirect you could write something like this:

public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
    if (loginInfo == null)
    {
        return RedirectToAction("Login");
    }
    var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);

    if(result==SignInStatus.Success)
    {
        var user=UserManager.Find(loginInfo.Login);
        returnUrl =UserManager.IsInRole(user.Id, "Administrator")
            ? "~/admin"
            : "~/dashboard";

    }
    // rest of code
}
Sam FarajpourGhamari
  • 14,601
  • 4
  • 52
  • 56