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.Crea te("https://account.authori ze.net/ui/themes/anet/Logon.aspx")
objRequest.Cook ieContainer = objCookieCont
'get initial Logon page
Dim objReader As New
IO.StreamReader (objRequest.Get Response().GetR esponseStream() )
Dim strResponseStri ng As String = objReader.ReadT oEnd()
objReader.Close ()
'parse page for VIEWSTATE value
Dim objRegEx As New Regex("__VIEWST ATE"" value="".*""")
Dim objMatch As Match = objRegEx.Match( strResponseStri ng)
If Not objMatch.Succes s Then _
Throw New ApplicationExce ption("Could not parse __VIEWSTATE
from Authorise.Net's Logon.aspx.")
Dim strViewState As String = objMatch.ToStri ng
strViewState = strViewState.Re place("__VIEWST ATE"" value=", "")
strViewState = strViewState.Re place("""", "")
'create data posting
Dim objConfig As Specialized.Nam eValueCollectio n =
ConfigurationMa nager.GetSectio n("CreditCardTr ans/Authorise.NET")
Dim strPostData As String =
String.Format(" __VIEWSTATE={0} &MerchantLogin= {1}&Password={2 }",
strViewState, objConfig("Logi n"), objConfig("Pass word"))
strPostData = HttpUtility.Url Encode(strPostD ata)
'create new request for credential submittion
objRequest =
WebRequest.Crea te("https://account.authori ze.net/ui/themes/anet/Logon.aspx")
With objRequest
.CookieContaine r = objCookieCont
.Method = "POST"
.ContentType = "applicatio n/x-www-form-urlencoded"
.ContentLength = strPostData.Len gth
End With
'add data post to request
Dim objWriter As New
IO.StreamWriter (objRequest.Get RequestStream() )
objWriter.Write (strPostData)
objWriter.Close ()
'get response
objReader = New
IO.StreamReader (objRequest.Get Response().GetR esponseStream() )
strResponseStri ng = objReader.ReadT oEnd()
objReader.Close ()
Response.Write( strResponseStri ng)
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.authori ze.net/ui/themes/anet/Logon.aspx">
<input type="hidden" name="__VIEWSTA TE"
value="dDw2ODc5 MDg1MDk7dDw7bDx pPDA+O2k8MT47aT w3Pjs+O2w8dDxwP Gw8VGV4dDs+O2w8 QXV0aG9yaXplLk5 FVCBXZWxjb21lOz 4+Ozs+O3Q8O2w8a TwwPjs+O2w8dDw7 bDxpPDA+Oz47bDx 0PHA8cDxsPFZpc2 libGU7PjtsPG88Z j47Pj47PjtsPGk8 MT47aTwzPjtpPDU +O2k8Nj47PjtsPH Q8cDxsPGhyZWY7P jtsPC91aS90aGVt ZXMvYW5ldC9sb2d vbi5hc3B4P3N1Yj 1sb2dvdXQ7Pj47O z47dDxwPGw8aHJl Zjs+O2w8amF2YXN jcmlwdDogdmFyIH dpbiA9IHdpbmRvd y5vcGVuKCcvdWkv dGhlbWVzL2FuZXQ vQ29udGFjdFVzL0 ZlZWRiYWNrLmFzc HgnLCAnRmVlZGJh Y2snLGNvbmZpZz0 naGVpZ2h0PTUwMC x3aWR0aD03NTAsc 2Nyb2xsYmFycz0w LCByZXNpemFibGU 9MScpXDs7Pj47Oz 47dDxwPGw8aHJlZ js+O2w8amF2YXNj cmlwdDogdmFyIHd pbiA9IHdpbmRvdy 5vcGVuKCcvdWkvd GhlbWVzL2FuZXQv Q29udGFjdFVzL1N 1cHBvcnQuYXNweC csJ3N1cHBvcnQnL GNvbmZpZz0naGVp Z2h0PTUwMCx3aWR 0aD03NTAsc2Nyb2 xsYmFycz0wLCByZ XNpemFibGU9MScp Oz4+Ozs+O3Q8O2w 8aTwxPjs+O2w8dD xwPGw8aHJlZjs+O 2w8amF2YXNjcmlw dDp2YXIgd2luID0 gd2luZG93Lm9wZW 4oJy91aS90aGVtZ XMvYW5ldC9Db250 YWN0VXMvQ2hhdC5 hc3B4JywnY2hhdC csY29uZmlnPSdoZ WlnaHQ9MzUwLHdp ZHRoPTUwMCxzY3J vbGxiYXJzPTAsIH Jlc2l6YWJsZT0xJ ylcOzs+Pjs7Pjs+ Pjs+Pjs+Pjs+Pjt 0PDtsPGk8MT47Pj tsPHQ8cDxwPGw8V mlzaWJsZTs+O2w8 bzxmPjs+Pjs+Ozs +Oz4+Oz4+Oz7rZC EFmJBwE1vGe4gpM 1ANQ6UhNA=="
/>
<input type="hidden" name="MerchantL ogin" value="username ">
<input type="hidden" name="Password" value="password ">
</form>
<script language="javas cript">document .all.form1.subm it();</script>
Here I hardcoded the viewstate taken from login page just to test.
Thanks
Perry