Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Maintain Query String after Forms Authentication cookie expires

4.00/5 (1 vote)
5 Jul 2011CPOL 13K  
Maintain Query String after forms authentication cookie expires and the user is redirected to the login page.

When forms authentication cookie expires, the user will be redirected to the login page (which is set in the config file). After login, you can redirect the user to the page that he/she was in before expiration according to the ReturnUrl parameter provided by forms authentication. Using the code block below, you can also maintain the Query String of the page before expiration:


C#
string querystring = "";

foreach (string key in Request.Params.AllKeys)
{
    string value = Request.QueryString[key];
    if (!string.IsNullOrEmpty(value))
        querystring += "&" + key + "=" + value;
}
if (querystring.Length > 0)
    querystring = Server.UrlEncode(querystring.Remove(0, 1));

Response.Redirect(FormsAuthentication.LoginUrl + "?ReturnUrl=" + 
         System.Web.HttpContext.Current.Request.Url.AbsolutePath + "?" + querystring);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)