Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > posting login credentials and __VIEWSTATE using HttpWebRequest fails

Reply
Thread Tools

posting login credentials and __VIEWSTATE using HttpWebRequest fails

 
 
msnews.microsoft.com
Guest
Posts: n/a
 
      05-10-2006
I have been raking my brains on why this does not work. I get back the same
login screen again instead of the home page redirection which should occur
after a successful login:

'create a cookie container so it can be shared between requests
so that authentication can be maintained
Dim objCookieCont As New CookieContainer

'create request to get the __VIEWSTATE of Authorize.NET's
Logon.aspx page
Dim objRequest As HttpWebRequest =
WebRequest.Create("https://account.authorize.net/ui/themes/anet/Logon.aspx")
objRequest.CookieContainer = objCookieCont

'get initial Logon page
Dim objReader As New
IO.StreamReader(objRequest.GetResponse().GetRespon seStream())
Dim strResponseString As String = objReader.ReadToEnd()
objReader.Close()

'parse page for VIEWSTATE value
Dim objRegEx As New Regex("__VIEWSTATE"" value="".*""")
Dim objMatch As Match = objRegEx.Match(strResponseString)
If Not objMatch.Success Then _
Throw New ApplicationException("Could not parse __VIEWSTATE
from Authorise.Net's Logon.aspx.")
Dim strViewState As String = objMatch.ToString
strViewState = strViewState.Replace("__VIEWSTATE"" value=", "")
strViewState = strViewState.Replace("""", "")

'create data posting
Dim objConfig As Specialized.NameValueCollection =
ConfigurationManager.GetSection("CreditCardTrans/Authorise.NET")
Dim strPostData As String =
String.Format("__VIEWSTATE={0}&MerchantLogin={1}&P assword={2}",
strViewState, objConfig("Login"), objConfig("Password"))
strPostData = HttpUtility.UrlEncode(strPostData)

'create new request for credential submittion
objRequest =
WebRequest.Create("https://account.authorize.net/ui/themes/anet/Logon.aspx")
With objRequest
.CookieContainer = objCookieCont
.Method = "POST"
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = strPostData.Length
End With

'add data post to request
Dim objWriter As New
IO.StreamWriter(objRequest.GetRequestStream())
objWriter.Write(strPostData)
objWriter.Close()

'get response
objReader = New
IO.StreamReader(objRequest.GetResponse().GetRespon seStream())
strResponseString = objReader.ReadToEnd()
objReader.Close()
Response.Write(strResponseString)

What is strange is that the following html works (just to prove that this
site can be logged into) however I can not use it like this because I need
to do multiple followup calls after authentication which need to use the
same session cookie:

<form name="form1" method="post"
action="https://account.authorize.net/ui/themes/anet/Logon.aspx">
<input type="hidden" name="__VIEWSTATE"
value="dDw2ODc5MDg1MDk7dDw7bDxpPDA+O2k8MT47aTw3Pjs +O2w8dDxwPGw8VGV4dDs+O2w8QXV0aG9yaXplLk5FVCBXZWxjb 21lOz4+Ozs+O3Q8O2w8aTwwPjs+O2w8dDw7bDxpPDA+Oz47bDx 0PHA8cDxsPFZpc2libGU7PjtsPG88Zj47Pj47PjtsPGk8MT47a TwzPjtpPDU+O2k8Nj47PjtsPHQ8cDxsPGhyZWY7PjtsPC91aS9 0aGVtZXMvYW5ldC9sb2dvbi5hc3B4P3N1Yj1sb2dvdXQ7Pj47O z47dDxwPGw8aHJlZjs+O2w8amF2YXNjcmlwdDogdmFyIHdpbiA 9IHdpbmRvdy5vcGVuKCcvdWkvdGhlbWVzL2FuZXQvQ29udGFjd FVzL0ZlZWRiYWNrLmFzcHgnLCAnRmVlZGJhY2snLGNvbmZpZz0 naGVpZ2h0PTUwMCx3aWR0aD03NTAsc2Nyb2xsYmFycz0wLCByZ XNpemFibGU9MScpXDs7Pj47Oz47dDxwPGw8aHJlZjs+O2w8amF 2YXNjcmlwdDogdmFyIHdpbiA9IHdpbmRvdy5vcGVuKCcvdWkvd GhlbWVzL2FuZXQvQ29udGFjdFVzL1N1cHBvcnQuYXNweCcsJ3N 1cHBvcnQnLGNvbmZpZz0naGVpZ2h0PTUwMCx3aWR0aD03NTAsc 2Nyb2xsYmFycz0wLCByZXNpemFibGU9MScpOz4+Ozs+O3Q8O2w 8aTwxPjs+O2w8dDxwPGw8aHJlZjs+O2w8amF2YXNjcmlwdDp2Y XIgd2luID0gd2luZG93Lm9wZW4oJy91aS90aGVtZXMvYW5ldC9 Db250YWN0VXMvQ2hhdC5hc3B4JywnY2hhdCcsY29uZmlnPSdoZ WlnaHQ9MzUwLHdpZHRoPTUwMCxzY3JvbGxiYXJzPTAsIHJlc2l 6YWJsZT0xJylcOzs+Pjs7Pjs+Pjs+Pjs+Pjs+Pjt0PDtsPGk8M T47PjtsPHQ8cDxwPGw8VmlzaWJsZTs+O2w8bzxmPjs+Pjs+Ozs +Oz4+Oz4+Oz7rZCEFmJBwE1vGe4gpM1ANQ6UhNA=="
/>
<input type="hidden" name="MerchantLogin" value="username">
<input type="hidden" name="Password" value="password">
</form>
<script language="javascript">document.all.form1.submit(); </script>

Here I hardcoded the viewstate taken from login page just to test.

Thanks
Perry



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"The credentials supplied conflict with an existing set of credentials" -=rjh=- NZ Computing 2 07-15-2006 11:09 PM
Re: httpwebrequest credentials Scott Allen ASP .Net 1 09-27-2004 05:09 PM
Setting HttpWebRequest Credentials to be current user Jamie ASP .Net 4 03-01-2004 09:51 PM
HttpWebRequest and posting login data buran ASP .Net Security 7 10-11-2003 09:51 AM
HttpWebRequest and posting login data buran ASP .Net 7 10-11-2003 09:51 AM



Advertisments