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

Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
  • Look at [this](http://stackoverflow.com/a/22262976/1768303). It works well for a console app and I expect it to work under ASP.NET as well, although I haven't tested it. – noseratio Jul 11 '15 at 05:36

2 Answers2

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", "test@email.com" },
                      { "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.

serhiyb
  • 4,753
  • 2
  • 15
  • 24
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

Andre
  • 1
  • 1
  • Thanks Andre for Giving reply but I tried this http://stackoverflow.com/questions/9123159/how-to-login-to-yahoo-website-programatically but I again reach at login page instead of Home page after login page – Riddhi Bhatt Jul 11 '15 at 04:34
  • 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. – Riddhi Bhatt Jul 11 '15 at 04:40
  • 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? – Andre Jul 11 '15 at 04:58