Front Controller Pattern/URL Rewriting - Invalid Viewstate

nikolaiblackienikolaiblackie Posts: 20
edited March 28, 2006 5:23PM in ANTS Load
With our web applications we have implemented a front controller/URL rewriting ISAPI DLL that will take URLs like http://yoursite/personedetails and automatically redirect to the actual page i.e. http://yoursite/pages/personedetails.aspx

I am attempting to create a script but I keep getting "Authentication of viewstate failed." errors. I have read in the forums that you need to specify an ASPX page for each request. Is this the case or are there any other ways around this requirement?

Comments

  • My script is pretty simple, it GETS the login page, then POSTS. When I review the IIS logs I see that it always throws a 500 on the POST. I added logging to the script and both values are null. I am assuming this maybe something to do with having no page extentions on request paths
    WebClient.HttpRequest.ProtocolVersion = "HTTP/1.1"
    BeginPage("http://localhost/")
    WebClient.HttpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 1.0.3705; ANTSLOAD)"
    WebClient.HttpRequest.Headers.Set("UA-CPU", "x86")
    WebClient.Get("http://localhost/")
    EndPage("http://localhost/")
    ' End of page 1 contains 1 request(s)
    
    Me.LogToFile(WebClient.ViewState) 'returns null
    Me.LogToFile(WebClient.HttpResponse.Location) 'returns null
    
    Sleep(7531)
    BeginPage("http://localhost/")
    WebClient.HttpRequest.Pragma = "no-cache"
    WebClient.HttpRequest.ContentType = "application/x-www-form-urlencoded"
    WebClient.HttpRequest.AddPostData("__EVENTTARGET", "Login1%3A_ctl0%3A_ctl7", False)
    WebClient.HttpRequest.AddPostData("__EVENTARGUMENT", "", False)
    WebClient.HttpRequest.AddPostData("__VIEWSTATE", WebClient.ViewState, False)
    WebClient.HttpRequest.AddPostData("Login1%3A_ctl0%3A_ctl3", "USERLOGINNAME", False)
    WebClient.HttpRequest.AddPostData("Login1%3A_ctl0%3A_ctl5", "USERPASSWORD", False)
    WebClient.Post("http://localhost/")
    WebClient.HttpRequest.ContentType = Nothing
    WebClient.Get("http://localhost/")
    EndPage("http://localhost/")
    ' End of page 2 contains 2 request(s).
    
  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    Hi,

    More than likely the problem has more to do with ANTS Load not following redirects (by design). What you want to do is capture the redirection URL programatically and follow it on:
    WebClient.HttpRequest.ProtocolVersion = "HTTP/1.1" 
    BeginPage("http://localhost/") 
    WebClient.HttpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 1.0.3705; ANTSLOAD)" 
    WebClient.HttpRequest.Headers.Set("UA-CPU", "x86") 
    WebClient.Get("http://localhost/") 
    EndPage("http://localhost/") 
    ' End of page 1 contains 1 request(s) 
    
    Me.LogToFile(WebClient.ViewState) 'returns null 
    Me.LogToFile(WebClient.HttpResponse.Location) 'returns null 
    
    Sleep(7531) 
    BeginPage("http://localhost/") 
    WebClient.HttpRequest.Pragma = "no-cache" 
    WebClient.HttpRequest.ContentType = "application/x-www-form-urlencoded" 
    WebClient.HttpRequest.AddPostData("__EVENTTARGET", "Login1%3A_ctl0%3A_ctl7", False) 
    WebClient.HttpRequest.AddPostData("__EVENTARGUMENT", "", False) 
    WebClient.HttpRequest.AddPostData("__VIEWSTATE", WebClient.ViewState, False) 
    WebClient.HttpRequest.AddPostData("Login1%3A_ctl0%3A_ctl3", "USERLOGINNAME", False) 
    WebClient.HttpRequest.AddPostData("Login1%3A_ctl0%3A_ctl5", "USERPASSWORD", False) 
    WebClient.Post("http://localhost/") 
    If WebClient.HttpResponse.Status=301 Or WebClient.HttpResponse.Status=302 Then
    WebClient.Get("http://localhost/"+WebClient.HttpResponse.Location)
    End If
    WebClient.HttpRequest.ContentType = Nothing 
    WebClient.Get("http://localhost/") 
    EndPage("http://localhost/")
    
  • Ok I have tried this, but why is it that after the first WebClient.Get("http://localhost/"), WebClient.ViewState still returns null? I know that the default page definately contains viewstate and this should be coming through. This is causing the login post to fail everytime.

    Do I need to look at using a get viewstate function like in this post? http://www.red-gate.com/messageboard/vi ... =viewstate
    i.e.
    Private Function GetViewState(ByVal content As String) As String
  • Well I managed to get my login script running, this was the final version that works for me.

    The key part is ensuring post data is encoded i.e. WebClient.HttpRequest.AddPostData("__VIEWSTATE", viewState, True)
    Private Function GetViewState(ByVal content As String) As String
            Try
                Dim i As Integer
                Dim iStart As Integer
                Dim iEnd As Integer
    
                Dim vstate As String = ""
                Dim contentToFind As String = "__VIEWSTATE"
    
                i = content.IndexOf(contentToFind) + contentToFind.Length + 1
    
                If i > 0 Then 'We have found viewstate in the page
                    iStart = content.IndexOf(Chr(34), i + 1) + 1
                    iEnd = content.IndexOf(Chr(34), iStart)
                    vstate = content.Substring(iStart, iEnd - iStart)
                End If
                GetViewState = vstate
            Catch ex As Exception
                Me.ReportError("GetViewState: " + ex.Message)
            End Try
        End Function
    
        Protected Overrides Sub Run()
            ' Script created with Internet Explorer on 29/03/2006 8:58:03 a.m.
    
            ' Random pause of between and 1 and 2 seconds
            ' (Remove this line if you want all scripts to start at the same time)
            RandomSleep(1000, 2000)
            Dim viewState As String
    
            WebClient.HttpRequest.ProtocolVersion = "HTTP/1.1"
            BeginPage("http://localhost/")
            WebClient.HttpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 1.0.3705)"
            WebClient.HttpRequest.Headers.Set("UA-CPU", "x86")
            WebClient.Get("http://localhost/")
            EndPage("http://localhost/")
            ' End of page 1 contains 4 request(s).
    
            Me.LogToFile("WebClient.ViewState 1st request")
            Me.LogToFile(WebClient.ViewState)
            Me.LogToFile("WebClient.HttpResponse.Location 1st request")
            Me.LogToFile(WebClient.HttpResponse.Location) 
            Me.LogToFile("GetViewState 1st request")
            viewState = GetViewState(WebClient.HttpResponse.Content)
            Me.LogToFile(viewState)
    
            Sleep(7156)
            BeginPage("http://localhost/")
            WebClient.HttpRequest.Pragma = "no-cache"
            WebClient.HttpRequest.ContentType = "application/x-www-form-urlencoded"
            WebClient.HttpRequest.AddPostData("__EVENTTARGET", "Login1%3A_ctl0%3A_ctl7", False)
            WebClient.HttpRequest.AddPostData("__EVENTARGUMENT", "", False)
            WebClient.HttpRequest.AddPostData("__VIEWSTATE", viewState, True)
            WebClient.HttpRequest.AddPostData("Login1%3A_ctl0%3A_ctl3", "******", False)
            WebClient.HttpRequest.AddPostData("Login1%3A_ctl0%3A_ctl5", "****", False)
            WebClient.Post("http://localhost/")
            If WebClient.HttpResponse.Status = 301 Or WebClient.HttpResponse.Status = 302 Then
                Try
                    WebClient.Get("http://localhost/" + WebClient.HttpResponse.Location)
                Catch ex As Exception
                    Me.ReportError(ex.Message)
                End Try
            End If
            WebClient.HttpRequest.ContentType = Nothing
            WebClient.Get("http://localhost/")
            EndPage("http://localhost/")
            ' End of page 2 contains 2 request(s).
        End Function
    
Sign In or Register to comment.