Python 3.4 login on aspx
Question: I'm trying to login to an aspx page then get the contents of another page as a
logged in user.

    
    
    import requests
    from bs4 import BeautifulSoup
    
    URL="https://example.com/Login.aspx"
    durl="https://example.com/Daily.aspx"
    
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36'
    language = 'en-US,en;q=0.8'
    encoding = 'gzip, deflate'
    accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
    connection = 'keep-alive'
    headers = {
        "Accept": accept,
        "Accept-Encoding": encoding,
        "Accept-Language": language,
        "Connection": connection, 
        "User-Agent": user_agent
    }
    
    username="user"
    password="pass"
    
    s=requests.Session()
    s.headers.update(headers)
    
    r=s.get(URL)
    print(r.cookies)
    soup=BeautifulSoup(r.content)
    
    LASTFOCUS=soup.find(id="__LASTFOCUS")['value']
    EVENTTARGET=soup.find(id="__EVENTTARGET")['value']
    EVENTARGUMENT=soup.find(id="__EVENTARGUMENT")['value']
    VIEWSTATEFIELDCOUNT=soup.find(id="__VIEWSTATEFIELDCOUNT")['value']
    VIEWSTATE=soup.find(id="__VIEWSTATE")['value']
    VIEWSTATE1=soup.find(id="__VIEWSTATE1")['value']
    VIEWSTATE2=soup.find(id="__VIEWSTATE2")['value']
    VIEWSTATE3=soup.find(id="__VIEWSTATE3")['value']
    VIEWSTATE4=soup.find(id="__VIEWSTATE4")['value']
    VIEWSTATEGENERATOR=soup.find(id="__VIEWSTATEGENERATOR")['value']
    
    login_data={
                "__LASTFOCUS":"",
                "__EVENTTARGET":"",
                "__EVENTARGUMENT":"",
                "__VIEWSTATEFIELDCOUNT":"5",
                "__VIEWSTATE":VIEWSTATE,
                "__VIEWSTATE1":VIEWSTATE1,
                "__VIEWSTATE2":VIEWSTATE2,
                "__VIEWSTATE3":VIEWSTATE3,
                "__VIEWSTATE4":VIEWSTATE4,
                "__VIEWSTATEGENERATOR":VIEWSTATEGENERATOR,
                "__SCROLLPOSITIONX":"0",
                "__SCROLLPOSITIONY":"100",
                "ctl00$NameTextBox":"",
                "ctl00$ContentPlaceHolderNavPane$LeftSection$UserLogin$UserName":username,
                "ctl00$ContentPlaceHolderNavPane$LeftSection$UserLogin$Password":password,
                "ctl00$ContentPlaceHolderNavPane$LeftSection$UserLogin$LoginButton":"Login",
                "ctl00$ContentPlaceHolder1$RetrievePasswordUserNameTextBox":"",
                "hiddenInputToUpdateATBuffer_CommonToolkitScripts":"1"
    }
    
    r1=s.post(URL, data=login_data)
    print (r1.cookies)
    
    d=s.get(durl)
    print (d.cookies)
    
    dsoup=BeautifulSoup(r1.content)
    print (dsoup)
    

but the thing is that the cookies are not preserved into the session and I
can't get to the next page as a logged in user.

Can someone give me some pointers on this. Thanks.


Answer: When you post to the login page:

    
    
    r1=s.post(URL, data=login_data)
    

It's likely issuing a redirect to another page. So the response to the POST
request returns the cookies in the response, then it redirects to another
page. The redirect is what is captured in r1 and does not contain the cookies.

Try the same command but not allowing redirects:

    
    
    r1 = s.post(URL, data=login_data, allow_redirects=False)
    

