Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormsAuthentication.RedirectFromLoginPage Vs Response.Redirect

This is with reference to a question asked here Login user after signup and here FormsAuthentication.RedirectFromLoginPage reload page.

Though I have answered the first question but i will admit that it's just "programming by co-incidence". If you see, answers to above two questions contradict each other but still both worked for respective users.

I would like to know what is the exact difference between this

FormsAuthentication.SetAuthCookie(USER_NAME, true);

Response.Redirect("copyPastPage.aspx"); 

And this FormsAuthentication.RedirectFromLoginPage(mainSignUp.UserName, true);

In terms of usage, we can see a logical difference that Response.Redirect can allow redirecting to any URL as against RedirectFromLoginPage would redirect only to the referrer. But that's usage difference.

Is there any fundamental diffence in their way of execution? If not, any thoughts why one would work at times and why other at times? what exactly happens under the hood of each of them?

I have google a bit but could not get any concrete answer.

like image 791
Subhash Dike Avatar asked Apr 12 '11 10:04

Subhash Dike


1 Answers

If you look at the code in RedirectFromLoginPage it does essentially the same

  • SetAuthCookie
  • get return URL from query string
  • Clear return URL

Here is a snippet:

    HttpContext current = HttpContext.Current;
    string returnUrl = GetReturnUrl(true);
    if (CookiesSupported || IsPathWithinAppRoot(current, returnUrl))
    {
        SetAuthCookie(userName, createPersistentCookie, strCookiePath);
        returnUrl = RemoveQueryStringVariableFromUrl(returnUrl, FormsCookieName);
        if (!CookiesSupported)
        {
            int index = returnUrl.IndexOf("://", StringComparison.Ordinal);
            if (index > 0)
            {
                index = returnUrl.IndexOf('/', index + 3);
                if (index > 0)
                {
                    returnUrl = returnUrl.Substring(index);
                }
            }
        }

But it does also check for support for cookie.

like image 167
Aliostad Avatar answered Sep 28 '22 07:09

Aliostad