Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

Automating forms authentication in ASP.NET

I was working on a screen scrapping project, and was looking for away to automate the login form for an ASP.NET web page. Googling around, I found a very helpful article written by Scott Allen that lists the steps necessary to automate login using HttpWebRequest.

The first step of automating the login programmatically involved scrapping the hidden Viewstate field sent back to the client from the login page response. The article above listed the following Non-RegEx method to scrape:

private string ExtractViewState(string s)

{

    string viewStateNameDelimiter = "__VIEWSTATE";

    string valueDelimiter = "value=\"";

 

    int viewStateNamePosition =

        s.IndexOf(viewStateNameDelimiter);

    int viewStateValuePosition = s.IndexOf(

          valueDelimiter, viewStateNamePosition

       );

 

    int viewStateStartPosition =

        viewStateValuePosition + valueDelimiter.Length;

    int viewStateEndPosition =

        s.IndexOf("\"", viewStateStartPosition);

 

    return HttpUtility.UrlEncodeUnicode(

        s.Substring(viewStateStartPosition,       

        viewStateEndPosition - viewStateStartPosition));

}

The above code can be simply replaced by a one line using the following Regular Expression:

"(?<=__VIEWSTATE\" value=\")(?<val>.*?)(?=\")"

So the above ExtractViewState function would turn into a one liner like this:

private string ExtractViewState( string s)

{

    return HttpUtility .UrlEncodeUnicode( Regex .Match(s,

        "(?<=__VIEWSTATE\" value=\")(?<val>.*?)(?=\")" )

.Groups[ "val" ].Value);

}

 

ahhh... the power of RegEx!

kick it on DotNetKicks.com

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author