简体   繁体   English

经过身份验证的ASP.NET MVC表单身份验证时重定向用户

[英]redirect user when Authenticated asp.net mvc forms Authentication

Im building a asp.net mvc 4 app. 我正在构建一个asp.net mvc 4应用程序。 Im using forms auth. 我正在使用表单身份验证。 I have set roles to users and Authenticated the controllers for certain roles but im trying to figure out how to redirect a user when he/she logs in I just want the user directed to a certain area? 我已经为用户设置了角色,并为某些角色认证了控制器,但是我试图弄清楚如何在用户登录时重定向用户,我只希望用户定向到某个区域? Im thinking its something to do with login action on the account controller. 我认为这与帐户控制器上的登录操作有关。 would a switch statement here on the roles and redirect to action on in different cases? 会在这里对角色进行声明,然后在不同情况下重定向到操作吗?

If you use the default MVC project in Visual Studio, there is already a redirect going on (to the previous page): 如果您在Visual Studio中使用默认的MVC项目,则已经存在重定向(转到上一页):

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
//[RequireHttps]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
    {
        return RedirectToLocal(returnUrl);
    }

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View(model);
}

If you want it to redirect to your own page, just replace the return RedirectToLocal(returnurl) by return RedirectToAction("Index", "Home"); 如果您希望它重定向到您自己的页面,只需将return RedirectToLocal(returnurl)替换为return RedirectToAction("Index", "Home"); with Index being your actionname and Home being your controller. 其中Index为您的动作名称,Home为您的控制者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM