Click here to Skip to main content
Click here to Skip to main content

Tagged as

Programmatic Login to CodeProject.com Website

, 11 Sep 2012
Rate this:
Please Sign up or sign in to vote.
How to login into a website programatically.

Introduction

In this article I will demonstrate to you how to login into a website programmatically using plain HTML  

Background 

While implementing a Single-Sign-On functionality, we need to login into other web applications without manually entering the credentials. The real problem occurs when we need to login into third party websites where we do not have access to the website code and database. In that case you need to simulate the login procedure programmatically on the client side itself so that all the client side cookies and other authentication stuff is created on the client machine itself.

Using the code

Using this approach, you can login into any website programmatically. I am using codeproject.com website for the demonstration.

The first step is to get the HTML source for the login page from the source of the webpage. I am using the HTML for these three HTML elements 'Email', 'Password', and 'SignIn' button. Rightclick on the webpage > View Source and copy the HTML and JavaScript (if any) related to Login functionality. 

 

This is the HTML I picked from the page source. 

<script type="text/javascript">        //<!-- 
        function doSubmit(secure) {
            if (secure)
                document.subForm.action = "https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2f"
            else
                document.subForm.action = "https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2f"
            document.subForm.submit();
            return true;
        } //--> 
	</script> 
	
	<a name="SignUp"></a> 
	<form name="subForm" id="subForm" 
	   action="https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2f" 
	   method="post" class="tight"> 
	
	<input id="FormName" name="FormName" 
	   value="MenuBarForm" type="hidden" /> 
	
	Email <input class="small-text" type="text" 
	  name="Email" id="Text1" style="width:150px" /> 
	Password <input class="small-text" type="password" 
	  name="Password" id="Password1" style="width:60px" /> 
	<input type="submit" value="Sign in" class="button" 
	  onclick="return doSubmit(false);" />

We need to do a little clean-up of the HTML obtained. Here is the HTML I retained after cleaning the unwanted elements.

<input type="text" name="Email" id="Email" style="width: 150px" /> 
 Password 
<input type="password" name="Password" id="Password" style="width: 60px" /> 
<input type="submit" value="Sign in" class="button" />

In the JavaScript function doSubmit() above, you can see that the page is submitted to the URL https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2f. Now we need to simulate the POST action to be fired on the same URL from our code. For this, I will make the following change to my HTML form tag like this.

<form method="post" action="https://www.codeproject.com/script/Membership/LogOn.aspx?rp=%2f">

This is a important step to note. With this change, my HTML page will not post to my server, but will post to the URL mentioned in the 'action'. 

My login form will look like this in the browser 

Enter your codeproject.com credentials in this form and click the Sign In button. You will be redirected to codeproject.com and will be logged into the website.

This approach can be used to login into any website which uses a username-password combination for authentication. What you need to login is the HTML source of the login page of the website you need to login and fire the HTTP POST action on the login URL of the destination website. You need to find out where the POST action is fired since the URL of the login page is not necessarily the URL where the POST is fired similar to the one demonstrated here. 

You can download the attached HTML page for a quick demonstration of programmatic logging into codeproject.com . 

Note: I am a member of codeproject.com since the last 6 years and have been answering the quick questions and sometimes posting questions. I am a regular reader of codeproject.com weekly newsletters. But this is my first article on codeproject.com. Please let me know your suggestions to improve my article. 

History

11 Sep 2012- Article submitted to codeproject.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

aspnet -i
Web Developer
India India
Software developer by profession, working for a service and product based organisation in India.
 
Career graph:
Software Programmer since 2002.
Web Developer in ASP.NET since 2004.
 
Interests:
I love reading the blogs and articles of technology experts. I love codeproject and stackoverflow .
 
I love to share knowledge and help the programmers. I appreciate if some body corrects my code or my concepts which helps me learn.

Comments and Discussions

 
QuestionHow about this one? PinmemberTYeeTY4-May-13 8:05 
AnswerRe: How about this one? Pinmemberaspnet_regiis -I4-May-13 8:58 
GeneralMy vote of 5 Pinmemberaspnet_regiis -i11-Dec-12 5:31 
QuestionSuggestion Pinmemberpocky617-Sep-12 0:55 
SuggestionThis is not an article. PinmemberSlacker00711-Sep-12 5:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 11 Sep 2012
Article Copyright 2012 by aspnet -i
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid