Question

I have a MVC 3 web application (more like a sub-application, really) that does not rely on ASPNET for authentication because that is already being used in another area of the app and we are using a different type of credentialing. At any rate, I decided to create my own authentication system with its own SQL table, which is working fine on one form in the app. Now, I am trying to roll it into the shared layout, calling a "Login" action in the home controller. The problem I am having is how to return from that Login action back to wherever the user is. When I try "return View();" it understandably tries to return to a Home/Login view, which doesn't exist. How do I return from this Action to wherever I was before the action? All views use the shared layout, so I'm hoping this is fairly simple!

public class HomeController : Controller       
{

 .....

    private DataModel db = new DataModel();

    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Login()
    {

        string un = Request["form_un"];
        string pc = Request["form_pc"];
        int usercount = db.JobBoardUsers.Where(u => u.userid.Equals(un) && u.passcode.Equals(pc)).Select(u => new AccountModel() { uid = u.id }).Count();
        usercount = db.JobBoardUsers.Where(u => u.userid.Equals(un) && u.passcode.Equals(pc)).Count();
        if (usercount > 0)
        {
            HttpContext.Session["IsSignedIn"] = true;
            HttpContext.Session["IsSignedInId"] = un;
        }
        return View();
    }
Was it helpful?

Solution

Instead of return View(), You need to use return RedirectToAction or return Redirect method.

You may get the previous page in your login action using different methods

1) pass the current pages url to login action method as a parameter and use that for redirecting after successful authentication

2)Use the Request object to get the UrlReferrer property value and use that as the page to be redirected.

Chek this blog post where they explains how they did it in nerddinner.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top