tags:

views:

126

answers:

2

I have a UserController that have methods like Register, Login, Logout, etc.

On my site I have a small login form that i've made as a partial view and is part of a masterpage. The behaviour I want is to be able to login from any view and then return to the controller I was at when i called the Login method.

I can call the methods from anywhere just fine, but can't figure out what to do to find out what controller and action the user was at to reroute back.

A: 

Use the Referer header from the HTTP Request. In PHP you get it with $_SERVER['HTTP_REFERER']; I don't know how it's done in ASP.NET, but it shouldn't be too hard if you google for "HTTP Header Referer".

Bart van Heukelom
A: 

Referer is not guaranteed to be populated, since some proxies do not send it. So I would recommend against depending on it.

Instead, when you redirect a user from a protected page to login page, save where they were into the Session object, or Viewdata, or maybe TempData object. So you can use the value in there to redirect them back to where they were when they successfully log in.

çağdaş
I've done the session way, but it's horribly unstable (e.g. when having multiple tabs of the same site). The Referer should be populated by a proper client. Otherwise you shouldn't have to serve it. Just like you kick away a client that attempts to use the "HTP" protocol or some other error.
Bart van Heukelom