Click here to Skip to main content
15,891,951 members
Articles / Web Development / ASP.NET
Tip/Trick

Maintain Query String after Forms Authentication cookie expires

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
5 Jul 2011CPOL 12.8K   4  
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)


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
I hold a BS degree in software engineering and am a Microsoft Certified Solution Developer(MCSD).
I have more than 8 years of experience in .NET developement, mostly web develop using C# and ASP.NET.

Comments and Discussions

 
-- There are no messages in this forum --