1

I have performed below process in VB.Net to login into website from my project ! Here I have taken webBrowser Control and its named with webBrowser1

private void button1_Click(object sender, EventArgs e)

{

navigate("http://www.websitename.com");

 webBrowser1.Document.GetElementById("WebpageTextBoxNameForUserName")
.InnerText = "MyUserName";

 webBrowser1.Document.GetElementById("WebpageTextBoxNameForPassword")
.InnerText = "MyPassword";

webBrowser1.Document.GetElementById("WebpageButtonNameToLogin")
.InvokeMember("click");

}

public void navigate(string url)

{

 webBrowser1.Navigate(url);            
     while(webBrowser1.ReadyState !=  WebBrowserReadyState.Complete)
     {
          Application.DoEvents();

     }

}

now I want to follow same procedure in ASP.Net can You people please help me out here ?

Thanks

1
  • Look at this. It works well for a console app and I expect it to work under ASP.NET as well, although I haven't tested it. Commented Jul 11, 2015 at 5:36

2 Answers 2

2

Here is working example:

This class extends default .NET WebClient with ability to store cookies:

public class CookieAwareWebClient : WebClient
{
    public void Login(string loginPageAddress, NameValueCollection loginData)
    {
        var parameters = new StringBuilder();
        foreach (string key in loginData.Keys)
        {
            parameters.AppendFormat("{0}={1}&",
                HttpUtility.UrlEncode(key),
                HttpUtility.UrlEncode(loginData[key]));
        }
        parameters.Length -= 1;

        var request = (HttpWebRequest)WebRequest.Create(loginPageAddress);

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        var buffer = Encoding.ASCII.GetBytes(parameters.ToString());
        request.ContentLength = buffer.Length;
        using (var requestStream = request.GetRequestStream())
        {
            requestStream.Write(buffer, 0, buffer.Length);
        }

        var container = request.CookieContainer = new CookieContainer();

        using (var response = request.GetResponse())
        {
            CookieContainer = container;
        }
    }

    public CookieAwareWebClient(CookieContainer container)
    {
        CookieContainer = container;
    }

    public CookieAwareWebClient()
        : this(new CookieContainer())
    { }

    public CookieContainer CookieContainer { get; private set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = (HttpWebRequest)base.GetWebRequest(address);
        request.CookieContainer = CookieContainer;
        return request;
    }
}

You can use it like this:

    protected CookieAwareWebClient GetAuthenticatedClient()
    {
        var client = new CookieAwareWebClient();

        var loginData = new NameValueCollection
                {
                      { "Email", "[email protected]" },
                      { "Password", "testPassword" }
                };

        client.Login("https://myurl.com/login", loginData);

        return client;
    }

and then navigate to some internal page using:

        using (var client = GetAuthenticatedClient())
        {
            var html = client.DownloadString("https://myurl.com/internalPage");
            //do your stuff with received HTML here
        }

I'm successfully using that logic in my ASP.NET projects.

Sign up to request clarification or add additional context in comments.

1 Comment

How can I open the page in a new window?
0

Are you trying to emulate user activity of input a username and password?

Why don't you try to use a POST or GET method (usualy for authentication web sites use POST) to achieve your goal.

You can use HTTPClient methode for that purpose

3 Comments

Thanks Andre for Giving reply but I tried this stackoverflow.com/questions/9123159/… but I again reach at login page instead of Home page after login page
Actually I tried HTMLAgility, HttpWebRequest, Webclient, HttpWebResponse and NameValueCollection but no change in problem this all put me again on login page even I completed authentication step.
Ok then, try to check the POST value in your browser. In firefox, right click, inapect element, tab network. Than compare the POST value that genuine sent by user type and your asp.net emulate, does it equaly same POST?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.