登录 MVC5 时的路由角色

Routing Roles upon login MVC5

我是 MVC 的新手,我目前正在创建一个需要路由登录的 Web 应用程序。我有 3 个用户 - 管理员、配镜师和用户。登录后,我希望管理员被重定向到管理页面,眼镜商被重定向到眼镜商页面,用户被重定向到主页。 启动后,我创建了角色。我已将用户注册到角色并且工作正常。我现在要做的是在登录时路由角色。

启动验证:

        // Creating new instance of Role Manager
        var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));

        //Creating User role
        RoleManager.Create(new IdentityRole("User"));
        //Creating Admin role
        RoleManager.Create(new IdentityRole("Admin"));
        //Creating Optician role
        RoleManager.Create(new IdentityRole("Optician"));

账户管理员:

    // GET: /Account/Login
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    //
    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                if(User.IsInRole("Admin")){
                    return RedirectToAction("Index", "Admin");
                }
                else if (User.IsInRole("Optician"))
                {
                    return RedirectToAction("Index","Optician");
                }
                else
                {   // if role is User
                    return RedirectToLocal(returnUrl);
                }
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

路由配置:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

然而,当我以管理员或配镜师身份登录时,我被重定向到 - RedirectToLocal(returnUrl) 而不是 ("Index", "Admin") 或 ("Index", "Optician").提前为这个问题的简单性道歉,但我不确定我要去哪里 wrong.Should if 语句在 POST 登录中?任何建议将不胜感激。

我找出了哪里出错了。我需要创建一个重定向方法,因为用户尚未在登录阶段以其角色登录。登录后,用户将被定向到重定向方法,该方法根据他们的角色重定向他们。

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl, ApplicationUser user)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }


        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToAction("RedirectLogin"); 
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

    public ActionResult RedirectLogin(string returnUrl)
    {
        if(User.IsInRole("Admin")){
                    return RedirectToAction("Index", "Admin");
                }
                else if (User.IsInRole("Optician"))
                {
                    return RedirectToAction("Index","Optician");
                }
                else
                {
                    return RedirectToLocal(returnUrl);
                }
    }