473,767 Members | 2,198 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

posting login credentials and __VIEWSTATE using HttpWebRequest fails

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

May 10 '06 #1
0 4280

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1920
by: Ollie | last post by:
I have an aspx page that contains a web control that contains 2 text boxes and button to submit the text. I am trying to submit text and collect the response from a console application, but it is not working correctly it is just returning the original page I am expecting it to return the a subsequent page (when a successfull login has occurred), the code is shown below. Does anyone know why it is not working correctly? I used the...
1
7918
by: Tom Jones | last post by:
Hi, I am using the HttpWebRequest and HttpWebResponse classes to pull information from a web server on the internet. I have an account on one of the webservers that I need to log into programatically. The connection is not secure (not https, etc.). The login page just has simple name & password textboxes on it. Can someone please tell me how I can programatically login using the
9
3587
by: buran | last post by:
Dear ASP.NET Programmers, How can I post data to an ASP.NET login page and pass authentication? The login page uses forms authentication, users must supply usernames and password and have to click on a submit button. I want to automate this process by supplying values with HttpWebRequest and then download a file on the site. I think that I cannot invoke the submit button. Pleeeasee help, thanks in advance
8
5380
by: Pazza | last post by:
Hi, Is there a way to cause the form submit button click event handler to fire when posting to a aspx page using httpwebrequest. In my tests the load event fires but not by button click event. I am hoping to use this technique for a screen scraper but if I can't get the submit buttons click event handler to fire its no use. Thanks,
7
17333
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem to get it to work in Visual Basic. I tried coding it once myself from scratch and then modified a class that I found on a newsgroup (referenced below). Both seem to be doing the same thing and neither works (or rather, they seem to work but the...
0
12782
by: barrybevel | last post by:
Hi, I'm trying to login to the www.vodafone.ie website using HttpWebRequest. It works fine with IE/Firefox and the .NET Web Control too, just not with my code. I think it's a redirect 302 problem. I'm using this code in a ASP.NET 2.0 application just in case that matters, maybe someone knows a better way to do this?
1
2001
by: twebb72 | last post by:
Scratching my head for a while on this one... This project uses code from "Understanding HttpWebRequest CookieContainer?" post on this site. C#, asp.net 2.0 The following code supplies credentials to the login page properly, if I change the POST credentials, it responds with the invalid login page. On the second get request (after a successful login), it continually redirects to the main page as if the session was over. Here is the code......
5
3199
by: mcfly1204 | last post by:
I am attempting to use WebRequest to access a page that requires a login/password to access. My last WebRequest continues to timeout. Any help or thoughts would be appreciated. namespace FormsAuthTest { class Program { static void Main(string args) { HttpWebRequest request = null;
1
2550
by: prasadpanati | last post by:
Hi all, I a beginner in to asp.net and learning to use httpwebrequest. I am trying to login to orkut.com using the following code.... but unfortunatly i am unable to login to it Can some one please help me by correcting the code.... Thanks in advance... Dim cookieJar As CookieContainer = New CookieContainer Dim webReq As HttpWebRequest
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.