ASP.NET Weblogin Using Python

ASP.NET Weblogin Using Python websites usually send state data in requests and responses in order to keep track of the client’s UI state. An ASP.Net website store the data that you filled out in the previous pages in a hidden field called “__VIEWSTATE”. View State is a Page-Level State Management technique used to preserve the value of the Page and Controls between round trips. It View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.

The __VIEWSTATE information is passed with each POST request that the browser makes to the server and the web server decodes and loads the client’s UI state from this data, performs some processing, computes the value for the new view state based on the new values and renders the resulting page with the new view state as a hidden field.

If the __VIEWSTATE is not properly sent back to the server, a blank form will be displayed as a result because the server completely lost the client’s UI state. In order to crawl pages resulting from forms like this, we haveto make sure that the crawler is sending __VIEWSTATE data with its requests, otherwise the page will not load as expected.

Here, the python script tries to simulate the browser actions to login to an website. BeautifulSoup is used to scrap the the webpage to gather required information.

In this website, once the website login page is accessed a dynamic url is generated for entering login information.

New URL is present in the Response Header


Building your Spider

The steps that the spider should follow to simulate the browser action

from bs4 import BeautifulSoup
import requests

session = requests.Session()
r = session.get('')

print ("STATUS 1:",r.status_code)
print (r.history)
print ("REDIRECT 1: ",r.url)

loginpage = session.get(r.url)

soup = BeautifulSoup(loginpage.text,"html.parser")

Next  is to build the request data with necessary information required for POST request to the web server. Enable and inspect the browser debugger for required information for your weblogin.

Request Header information. In this example we are passing only the referer value in Header data.

# parse and retrieve two vital form values
viewstate ="#__VIEWSTATE")[0]['value']
eventvalidation ="#__EVENTVALIDATION")[0]['value']

item_request_body = {

print("VIEWSTATE : ",viewstate)
print("EVENTVALIDATION : ",eventvalidation)

POST operation

A Session POST request is invoked with the url, data and header parameters. __VIEWSTATE and __EVENTVALIDATION information is stored in item_request_body.  After successful login, the contents of the home page will stored in defaultpage variable.

print ("==========POST==============")

response =, data=item_request_body, headers={"Referer": r.url})

print ("STATUS 2:",response.status_code)
defaultpage = r.url[:-10]+"form_custom.aspx?param1=ASR&param2=New&param3=default"
print ("PAGE : ", defaultpage)

page = session.get(defaultpage)
print (page.content)