How to login into ASP.Net application programatically

Some time back I discussed How to submit requests to web sites programatically using HttpWebRequest. As I discussed in that article that back bone of the whole process is to know the structure of the page along with FORM parameters that need to be passed to target URL. Well that works for most of the application. You run into problem when you are dealing with ASP.Net because of its event model that gets used ASP.Net framework. If you follow the procedure discussed in last article, you will notice that most of the time you are not able to login into the site programatically. There is nothing wrong with the method that I discussed. There is little quirk for ASP.Net and that requires little understanding of how ASP.Net event mechanism works. So I will give brief description, you can read details in microsoft documentations

.

If you look at source of an ASP.Net page, you will notice a hidden input variable __EVENTTARGET. When you click on some control that triggers postback, this variable is used to store ID/Name of that control. And this variable gets passed on to server when FORM is submitted. ASP.Net framework looks at this variable to decide what event handler needs to be triggered.

Given that information, if you do not pass correct value of the control that triggers log in action, sending HttpWebRequest with just login credentials is not going to work at all. Because your request will go to server, and it will never reach the event handlers that handles click on that button. So when you are dealing with ASP.Net sites, check ID/Name of the button or control that user clicks to start login process. You will need to pass that ID/Name as additional FORM key-value pair in POST or GET request. Following code snippet shows that this is about.


reqAttribs.RequestParameters.Add("txtEmail", "xxxxxxxxx");
reqAttribs.RequestParameters.Add("txtPassword", "yyyyyyy");
reqAttribs.RequestParameters.Add("__EVENTTARGET", "btnSubmit");

Notice third request parameter that I added which is ID of input button control. Hope this information helps in understanding programatic login process for ASP.Net sites.

comments powered by Disqus

Search

Social

Weather

Monthly Posts