0

I call an Action from a Login controller to authenticate users, once the user is authenticated I would like to call either the Cashier or the Supervisor action, depending on the user's role, and display the appropriate view.

I can break on AuthenticateUserByCard but RedirectToAction doesn't seem to be working.

I'm not sure if what I'm trying to do is deviating from the MVC architecture, if so please suggest the correct way to do this

Login controller:

public class LoginController : Controller
    {
        public ViewResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AuthenticateUserByCard(string token)
        {
            //Authenticate user and redirect to a specific view based on the user role
            Role role = GetRoleByToken(token);

            if(role.UserType == UserType.Supervisor)
                return RedirectToAction("Supervisor", "Login", new { id = token });
            else
                return RedirectToAction("Cashier", "Login", new { id = token });

            return null;
        }

        public ActionResult Supervisor(string id)
        {
            //Do some processing and display the Supervisor View
            return View();
        }

        public ActionResult Cashier(string id)
        {
            //Do some processing and display the Cashier View
            return View();
        }
}

Java Script:

$.get("/Login/AuthenticateUserByCard",{token:token});
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
  • are you wanting to change the window url or just load the content into a container? if the latter, just return the call to the controller rather than redirect. If you want to change the url, try handling the onsuccess and looking for the 302 and see if it is being actioned by jquery – Slicksim Apr 03 '14 at 08:55
  • I want to change the url and display a different view, either Cashier or Supervisor.I would like to do this on the server and not in javascript if possible – Denys Wessels Apr 03 '14 at 08:59
  • I don't think 'Get' will not process a browser redirect (which is what `RedirectToAction` does). (I only just noticed how you were calling it). – iCollect.it Ltd Apr 03 '14 at 09:01
  • @TrueBlueAussie yes I've tried placing break points, they are not being hit and if I try access the views directly it's working – Denys Wessels Apr 03 '14 at 09:01
  • I tried $.post as well, same thing – Denys Wessels Apr 03 '14 at 09:02

1 Answers1

0

jQuery post and get ignore 301 browser redirects returned from the server. You would normally need to handle them yourself. This can get messy: How to manage a redirect request after a jQuery Ajax call

All you really need in this case is to return the choice of methods, but make them return explicit views (not implicit). The default would always be to return the view based on the IIS-called method i.e. "AuthenticateUserByCard" unless you specify the view.

e.g.

public class LoginController : Controller
{
    public ViewResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult AuthenticateUserByCard(string token)
    {
        //Authenticate user and redirect to a specific view based on the user role
        Role role = GetRoleByToken(token);

        if(role.UserType == UserType.Supervisor)
            return Supervisor(token);
        else
            return Cashier(token);

        return null;
    }

    public ActionResult Supervisor(string id)
    {
        //Do some processing and display the Supervisor View
        return View("Supervisor");
    }

    public ActionResult Cashier(string id)
    {
        //Do some processing and display the Cashier View
        return View("Cashier");
    }

This will not change the URL though. If you need that too try the other answer I linked. You basically handle the redirect in jQuery and goto the new page.

Alternatively, to change the URL, put the desired URL into a hidden field of the returned views and extract that value to update the browser URL (just a thought) :)

Community
  • 1
  • 1
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202