Get URL of Referer page in ASP.NET

Cyberpks picture Cyberpks · Sep 3, 2012 · Viewed 11.9k times · Source

I am working on an ASP.NET project and currently struck in something. Basically, I have three different levels of users 1. Admin 2. DMV 3. Caller

All the users have their respective directories, and can access the resources in those according to their rights. When someone wants to sign-in to the application, he has to use the same login.aspx page that is present in the root directory, and once logged-in, he is redirected to the home page of his respective folder, based on his privilege.

In my application, sometimes when a session closes unwillingly, the user is redirected to the login page, or he knows the complete url of the page and just want to quickly login and get to that page using a "Redirect_URL" variable passed as a query string, he can't do that. Here when he has logged in he is redirected to his home page, that will make him again do a number of actions to reach the page he was on.

So, I need the login.aspx page to check the URL of referrer page, store it in a query string say "Redirect_url" using something like HTTP_REFERER, and once the user has successfully logged in, he is redirected to the page, present in the "Redirect_url" variable.

I have also looked for the Global.asax solution given somewhere but in my case, I think the above mentioned logic will work best.

Answer

Dai picture Dai · Sep 3, 2012

Two things:

First: for a quick solution, just use Request.ServerVariables["HTTP_REFERER"] or Request.UrlReferrer to get the HTTP Referrer header value.

Second: Use ASP.NET's built-in Forms authentication classes, they can perform the redirection (and querystring redirect-to) management all for you.