Search code examples
javascriptashx

Where find URL in return of an ASHX returning RedirectFromLoginPage


I have an ashx receiving login/password and send error message if it's wrong but if it's good make a RedirectFromLoginPage.

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        FormsAuthentication.SignOut()
        Dim LesDatas As New MyEntities
        Dim username As String = GetParamURL("UserName")
        Dim password As String = GetParamURL("Password")

            If Not IdentifyCollab(username, password, LesDatas) Then
                FormsAuthentication.SignOut()
                context.Session.Abandon()
                HelperJournal.Log("Echec de connexion :" & username)
                loginStatut = LoginFailed(GetIPAddress, LesDatas)
                context.Response.ContentType = "text/plain"
                context.Response.Write(loginStatut.Msg)
            Else
                 FormsAuthentication.RedirectFromLoginPage(ConnectedUser.Login, False)
            End If
End Sub

If You use directly the ashx in browser url zone : http://localhost:5959/login/login.ashx?UserName=toto&Password=MyPass, you're directly redirect in the default page http://localhost:5959//choix.aspx

But If I call it from JS :

$.post("/Login/Login.ashx?UserName=" + mdp1 + "&Password=" + mdp2, {},
     function (response,tt,ti) {
        if (response.indexOf("html") === -1) $("#ResultConnexion").html(response); else {
           window.history.pushState("", "", '/choix.aspx');
           var newWindow = window.open("", "_self");
           newWindow.document.write(response);
           }
      });

I need to use pushState to correct the url in browser, and window.open/write to display the defaultpage. But like you can see, I write in hard "/choix.aspx", cause the url are not modify and stay on referer URL (my login page http://localhost:5959/Login/login.html) But If I can have automaticly the good URL when I use in browser my ashx, it's seems the URL information is sent.

In function use in response of $post I note severals parameters : response,tt,ti response is the text to display page in return tt="success" ti is an object with lot of informations... but I don't see where is URL. If someone can help me.


Solution

  • I just find a way here

    In my masterpage

     Private Sub Choix_Init(sender As Object, e As EventArgs) Handles Me.Init
        Response.AppendHeader("X-MONAPPLI", HttpContext.Current.Request.Url.PathAndQuery)
      
    End Sub
    

    and in login.js

     $.post("/Login/Login.ashx?UserName=" + mdp1 + "&Password=" + mdp2, {},
          function (response, tt, xhrreq) {
             if (response.indexOf("html") === -1) $("#ResultConnexion").html(response); else {
                 window.history.replaceState("", "", xhrreq.getResponseHeader("X-MONAPPLI"));
                  var newWindow = window.open("", "_self");
                 newWindow.document.write(response);
                 }
              });
    

    I use replaceState instead of pushstate cause I've secyrity error