4

I am having problems with redirecting from one controller to another, my ASP.NE MVC application starts on the login page, then moves to an otp page when the user successfully logged in (The LOGIN and OTP actions is in the same controller).

When the OTP was successfully submitted, then the application must continue to the menu page, but instead it redirects back to the login page.

AuthenticateController: Login action

// POST: /Authenticate/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(ViewModel_Login model)
{
        // do login validation
        if (loggedin)
        {
            return View("OTPAuthentication");
        }
        else
        {
            return View(model);
        }
}

AuthenticateController: OTPAuthentication action

// POST: /Authenticate/OTPAuthentication
[HttpPost]
[AuthorizeUser]
[ValidateAntiForgeryToken]
public ActionResult OTPAuthentication(ViewModel_OTP model)
{
        if (ModelState.IsValid)
        {
            // do OTP validation
            return this.RedirectToAction("MainMenu", "Options");
        }
        else
        {
            ModelState.AddModelError("", "The one time pin provided is incorrect.");
        }

        return View(model);
}

OptionsController: MainMenu action

// GET: /Options/MainMenu
[AuthorizeUser]
public ActionResult MainMenu()
{
        return View();
}

RouteConfig:

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

routes.MapRoute(
name: "Menu",
url: "Menu",
defaults: new { controller = "Options", action = "MainMenu" });

routes.MapRoute(
name: "Login",
url: "Login",
defaults: new { controller = "Authenticate", action = "Login" });

routes.MapRoute(
name: "OTP",
url: "OTP",
defaults: new { controller = "Authenticate", action = "OTPAuthentication" });
2
  • What is default view of MainMenu action? is it specified? Commented Mar 4, 2016 at 12:23
  • the MainMenu only contains 3 link buttons to a third controller Commented Mar 4, 2016 at 12:46

2 Answers 2

3

If you are using forms authentication, then you must have do something like this before redirecting user to MainMenu controller.

if (ModelState.IsValid)
   {
       string userName = "user123";
       FormsAuthentication.SetAuthCookie(userName , True)
       // do OTP validation
       return this.RedirectToAction("MainMenu", "Options");
    }
    else
    ....
Sign up to request clarification or add additional context in comments.

Comments

0

Thank you for your help.

I found my problem. Because I use my own login services I had to over wright the AuthorizeAttribute and on some custom authorize attribute tutorial they said i should include the following:

var isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized)
{
    return false;
}
// do try/catch that validate the session and the session security

so all I had to do was to remove this piece of code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.