Redirect to the previous page after login

asp.net, authentication, redirect

Solution

In each page check if user logged in, if not then

if (Session["UserName"] == null && Session["UserId"] == null) 
{
    string OriginalUrl = HttpContext.Current.Request.RawUrl; 
    string LoginPageUrl = "/login"; 
    HttpContext.Current.Response.Redirect(String.Format("{0}?ReturnUrl={1}", LoginPageUrl, OriginalUrl));
 }

and in login page check if `returnurl` there, If `returnurl` exist redirect to that page.

if (this.Request.QueryString["ReturnUrl"] != null)
{
  this.Response.Redirect(Request.QueryString["ReturnUrl"].ToString());
}
else
{
  this.Response.Redirect("/account/default");
}

Problem

I have created an Admin website using Asp.Net Web Forms. When I share a url of a page(not a home page) of my website with my friend, and when he enters it in his browser, it automatically redirects him to Login page.(Which is correct behavior). When he enters his username and password it redirects to the home page and not the url I shared with him. I tried using `Request.UrlReferrer.PathAndQuery` on login.aspx. It works only if user intentionally logged out of the system. Basically, I want to share a link(url) by mail or something, user will open it, he will ask for login if he is not already logged in, once logged in the browser will show him the page from the url and not the home page.

Original source