在ASP.net Identity中,如何在登录后将用户重定向到特定URL?

时间:2015-11-07 23:50:12

标签: c# asp.net asp.net-mvc asp.net-identity-2

我在我的应用程序中使用ASP.Net identity 2.0包。用户成功登录后,我想将它们重定向到站点上的特定URL。目前,在成功授权后,它会将它们发送回默认索引页面。

认为它位于AccountController.cs的这一部分:

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

        // This doen't count login failures towards lockout only two factor authentication
        // To enable password failures to trigger lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);
        var user = await UserManager.FindByNameAsync(model.UserName);

        switch (result)
        {
            case SignInStatus.Success:
                if (user.LoginStage <= 0)
                {
                    return RedirectToAction("EnableGoogleAuthenticator", "Manage");
                }
                else
                {
                    return RedirectToLocal(returnUrl);
                }
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: true);
                return View(model);
        }
    }

2 个答案:

答案 0 :(得分:3)

RedirectToLocal(returnUrl);更改为return RedirectToAction("Action", "Controller");

但这意味着你忽略了returnUrl

答案 1 :(得分:0)

我使用Response.Redirect("~/SomePage", false);