Error during FormsAuthentication.RedirectFromLoginPage

All replies

  • If I change functionality from

    FormsAuthentication.RedirectFromLoginPage(TxtUser.Text, false);





    This works...however I am still puzzled as to what is causing the recommended way to fail in my setup.

    Ross B.


    Monday, March 06, 2006 9:41 PM
  • Hi,


    I am facing a similar problem. However, I tried your solution but it did not work.

    In my case the http://<Site Name>/_layouts/login.aspx?ReturnUrl=%2flists%2ffeedback%2fnewform.aspx%3fsource%3d%2fpages%2fconfirmation.aspx%3fsource%3dhttp%3a%2f%2f<Site Name&source=/pages/confirmation.aspx?source=http://<Site Name>


    When I replace the return url from realtive to absolute link it starts working. Can you please suggest me where should i change to get this working?


    Thanks in advance,

    Tuesday, May 15, 2007 10:33 AM
  • Thank you Ross B.


    Friday, June 20, 2008 6:19 PM
  • The problem with your query strinng is the it contains the colon ":" somewhere in it.  That character (amongs other ones) will cause the error you got.  I ran into the same problem and this is how it was solved.


    Code Snippet



       FormsAuthentication.RedirectFromLoginPage(txtEmail.Text, false);


    catch (Exception ex)


       string strURL = (null != Request.QueryString["ReturnURL"] && Request.QueryString["ReturnURL"] != "") ?  Request.QueryString["ReturnURL"] : "Default.aspx";

       FormsAuthentication.SetAuthCookie(txtEmail.Text, false);










    Monday, November 03, 2008 7:07 PM
  • thanks lopez this fixed


    Tuesday, November 11, 2008 7:28 AM
  • I use VS 2012 and ASP.NET 4.6.1.

    I have this code:

         public partial class NsiPublic : Frk.Web.UI.MyBetterMasterPage
            protected void Page_Load(object sender, EventArgs e)
                if (!IsPostBack || !ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
                    var rootPath = ResolveUrl("~/").JsEncode();
                    var redirectUrl = System.Web.Security.FormsAuthentication.GetRedirectUrl("", false);
                    var returnUrl = ResolveUrl(redirectUrl).JsEncode();
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Js/login.js", 
                        string.Format("LoginClient.Init('{0}','{1}');", rootPath, returnUrl), true);

    I get the following **`System.Web.HttpException`** error: 

    > The return URL specified for request redirection is invalid

    Fails here:  

        var redirectUrl = System.Web.Security.FormsAuthentication.GetRedirectUrl("", false);

    **Troubleshooting values**


    > /v2/Comunication/AlertConviv.aspx?form=FRM_QUERYREC.fmx&amp;parameters=RECIBO:%20689993447875

    IMHO, some part of my return URL contained invalid characters. In my case, someone was creating a return URL where the query string was not properly encoded and it contained `:` and `/` which need to be encoded as `%3a` and `%2F` respectively.

    Maybe solution using `HttpUtility.UrlEncode` or use any fixed like

        string returnUrl = string.IsNullOrEmpty(Request.QueryString.Get("ReturnURL")) ?
        "Default.aspx" : Request.QueryString.Get("ReturnURL");

    Or Exception handling (I don't like much)

            var redirectUrl = System.Web.Security.FormsAuthentication.GetRedirectUrl("", false);
        catch (HttpExceptionex)
           string strURL = (null != Request.QueryString["ReturnURL"] && Request.QueryString["ReturnURL"] != "") ?  Request.QueryString["ReturnURL"] : "Default.aspx";

    Any **final solution** to follow **good patterns and practices** ?

    Thursday, July 14, 2016 8:55 AM