blah blah blah is here! blah blah » Close

up1down
link

Hi,

I am working on a simple application with a listbox and webbrowser control. I have two textboxes for username and password, and I have a button. I have some URLs in the listbox that have login forms. I need to get the username and password data from the textboxes to the related fields on the web page inside the webbrowser when the button is clicked.

I have been searching to find some samples for this but I couldn't find anything helpful. Could you direct me please.

Thanks.

last answered one year ago

4 answers

up1down
link

well its usually done by sending the info as a query string for some website. but in case of security you can do it in this way which might not work for some sites as well but worked for many sites. first open the login page and get all the data from it by saving it to your local pc by saving it as html. search for the place where the username textfield and password textfield are in html. set the username and password textfield value by making a value key in both fields like MYUSERNAMEFIELD and MYPASSWORDFIELD to the saved html document. then in your win form read the saved html file by System.IO.File.ReadAllText(htmlFilePath); and replace the html keys you put in the html files with the needed credentials. then add a script in the end of the webbrowser control content to click the login button when the whole html is loaded. if you find it a bit difficult set the whole thing in the html file so all you need to do is add a script in the end of the html file and set the usernamefield value to the needed credential and the same for the password and apply the login button click(), save the html file and then load the html file on the webbrowser control and it will automatically fill the textfields and the login button is clicked.

ademmeda
108

Thanks for the answer, as I am a total newbie it is difficult to do the exact steps you suggested in your answer, if you could lead me to an example form or code, I would be grateful. As the first step I just want to accomplish this: To pass the data from textbox(e.g., username) to the field on the web page when the button is clicked. No login required.

up1down
link

an example that will clear thing is this one:

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser1.DocumentText+="<script>alert('ok');</script>";// this is the script that will alert you when the page is loaded. now to change the input field, change the script to this: <script>document.getElementById('myInput').value='my value';</script> where myInput is the id of the field that you need to change.
}

void goToPage()
{
this.webBrowser1.Url = new Uri("http://www.google.com");
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}

up0down
link

Thanks again, but how can I combine the code you've given with the current one:

public Form1()
{
InitializeComponent();
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Navigate(new Uri(listBox1.SelectedItem.ToString()));
}


private void button2_Click(object sender, EventArgs e)
{

}

up1down
link

you can merge it like this:

public Form1()
{
InitializeComponent();
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Navigate(new Uri(listBox1.SelectedItem.ToString()));
}


private void button2_Click(object sender, EventArgs e)
{

}

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser1.DocumentText+="<script>alert('ok');</script>";// this is the script that will alert you when the page is loaded. now to change the input field, change the script to this: <script>document.getElementById('myInput').value='my value';</script> where myInput is the id of the field that you need to change.
}

ademmeda
108

Thanks again, I will need to work on this to figure it. It is strange that I can't find one single example on the web about auto login using C# web form

muster
1556

as u see from the example i gave u is that its not in the scope of c#, its about the html in the webpage, so all what you have to do is to control the html page by the scripts for example.

Feedback