Friday, July 4, 2008

Difference between Response.Redirect and Server.Transfer

I've a secured page that requires users to be logged in.
At the beginning of the page, checks if an user is authenticated and is in right role. When not authenticated, control is redirected to login.aspx which is included Login form.

if (!User.Identity.IsAuthenticated)
{
if (!User.IsInRole("admin"))
{
Server.Transfer("login.aspx");
}
}

This piece of code is telling you cannot see the page if you are not an administrator.
Another one is:

if (!User.Identity.IsAuthenticated)
{
if (!User.IsInRole("admin"))
{
Response.Redirect("login.aspx");
}
}

The difference is Redirect tells client to see login.aspx, Transfer directly executes login.aspx and returns result. I choose Server.Transfer, cus it can be faster.

One more thing:

if (!User.Identity.IsAuthenticated)
{
if (!User.IsInRole("admin"))
{
Response.Redirect("login.aspx?ref=any_page.aspx");
}
}
In login.aspx.cs:
LoginView1.DestinationPageUrl= Request.Params["ref"];

In this case, control jumps to any_page.aspx. I didn't experience it when using Server.Transfer.
I'm not sure its possible.