user2790781
user2790781

Reputation: 85

redirect user when Authenticated asp.net mvc forms Authentication

Im building a asp.net mvc 4 app. 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?

Upvotes: 0

Views: 418

Answers (1)

blubbiedevis
blubbiedevis

Reputation: 415

If you use the default MVC project in Visual Studio, there is already a redirect going on (to the previous page):

[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"); with Index being your actionname and Home being your controller.

Upvotes: 2

Related Questions