Ok, first off I want to say that I have no idea what I'm doing.
I have several pages in my MVC project that constantly get hit from a different page. For instance my Edit page for one of my Models can come from an admin link on a detail page or the Crud Index page. Once we are done editing I want to send the user back to the right place. Considering I don't understand the Identity returnUrl stuff, I wrote my own method that creates two cookies, eventually I may not een need two but for now I have two so that I can monitor them updating in Chrome F12 tools and make sure that each time I use this method in a Controller Action it's doing the right thing. I'd also like to say I'm not doing any testing for this either. My testing is trial and error and Chrome F12.
Here is the code:
private void LastActionHero()
{
string lastUrl = null;
var currentUrl = Request.Url.LocalPath;
if (Request.Cookies["currentUrl"] != null)
{
lastUrl = Request.Cookies["currentUrl"].Value;
}
if (lastUrl != currentUrl)
{
var current = new HttpCookie("currentUrl", currentUrl)
{Expires = DateTime.Now.AddDays(1)};
Response.Cookies.Set(current);
var previous = new HttpCookie("lastUrl", lastUrl)
{ Expires = DateTime.Now.AddDays(1) };
Response.Cookies.Set(previous);
}
}
Questions: Is there a better way of doing this and is this safe? A portion of the site is not behind a login. Does that matter?
Also do you have any ideas of how I should implement this?
I'm still working on how I'm going to use it.
Here is a screenshot of the cookies, they seem to be working fine I went back and forth several times amongst 3 or 4 actions all using the method, here it shows the correct last 2 Urls: