Reputation: 105
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" });
Upvotes: 4
Views: 2052
Reputation: 105
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.
Upvotes: 0
Reputation: 6999
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
....
Upvotes: 3