locked
Username and passwords using C#

    Question

  • Hey I wrote a Password manager IE plugin using C#. Now its fillinf up usernames fine everywhere. It also fills passwords well for many web pages but for some pages the passwords do not get filled at all. I am using HTMLDocumentClass.all.item("Email",0) etc. to fill usernames and passwords. Here is the code:

    string selectedItem = comboBoxChooseService.SelectedItem.ToString();
                        string password = AuthenticatePassword.getPassWord(selectedItem);
                        string username = AuthenticatePassword.getUserName(selectedItem);

                        HTMLDocumentClass doc = new HTMLDocumentClass();
                        doc = (HTMLDocumentClass)this.Explorer.Document;
                        Object input = doc.all.item("email", 0);

                        if (input == null)
                            input = doc.all.item("Email", 0);

                        if (input == null)
                            input = doc.all.item("username", 0);

                        if (input == null)
                            input = doc.all.item("Username", 0);

                        if (input == null)
                            input = doc.all.item("UserName", 0);

                        HTMLInputElement element = (HTMLInputElement)input;
                        element.value = username;

                        Object inputPassword = doc.all.item("passwd", 0);
                        HTMLInputElement elementPassword = (HTMLInputElement)inputPassword;

                        if (inputPassword == null)
                            inputPassword = doc.all.item("password", 0);

                        if (inputPassword == null)
                            inputPassword = doc.all.item("Password", 0);

                        if (inputPassword == null)
                            inputPassword = doc.all.item("PassWord", 0);
                       
                        elementPassword.value = password;


    What am I doing wrong? I have checked that all the pages I have tested on have their input password text items have names one of "PassWord" , "passwd" or "password". IS their a better way of doing it?
    Sunday, October 08, 2006 7:44 PM

Answers

  • Hi

    What if you change your procedure like this:

      Object inputPassword = doc.all.item("passwd", 0);
                        HTMLInputElement elementPassword = (HTMLInputElement)inputPassword; //Move this line

                        if (inputPassword == null)
                            inputPassword = doc.all.item("password", 0);

                        if (inputPassword == null)
                            inputPassword = doc.all.item("Password", 0);

                        if (inputPassword == null)
                            inputPassword = doc.all.item("PassWord", 0);

    //Here...
                        HTMLInputElement elementPassword = (HTMLInputElement)inputPassword;
                        elementPassword.value = password;

     

    Yours Markku

    Sunday, October 08, 2006 8:41 PM
  • Hi

    Sorry, but I can give some kind answer only on your first question. Sorry my badly english, I hope you understant what I try to explain

    This is part of your original code.

                        Object inputPassword = doc.all.item("passwd", 0); // inputPassword is null or reference to object name "passwrd"
                        HTMLInputElement elementPassword = (HTMLInputElement)inputPassword; // elementPassword is same value as inputPassword (null or reference to "passwrd")

                        if (inputPassword == null) // if inputPassword is null (elementPassword is null, too)...
                            inputPassword = doc.all.item("password", 0); // inputPassword is null or reference to object name "password", but elementPassword is null.

                       // ...

    So, your original code find all items ("passwd", "password" and "PassWord") but elementPassword have only two value (null or reference to "passwd").

     

    Yours Markku

    Sunday, October 08, 2006 10:01 PM
  • I just thing that you could scan all items and check HTMLInputElement.type == "password".

    I'm not sure but this might be one way to find those user passwords. Code would be somthing like this:

    foreach (object obj in doc.all)
    {
        if (obj is HTMLInputElement)
        {
            HTMLInputElementClass btn = (HTMLInputElementClass) obj;
            if (btn.type != null)
            {
                string type = btn.type.ToLower();
                if (type == "password")
                { .... OK .... }
            }
        }
    }

    ( I find this code from http://www.codeproject.com/csharp/ie_advanced_toolbar.asp )

    Yours Markku

    Sunday, October 08, 2006 10:44 PM

All replies

  • Hi

    What if you change your procedure like this:

      Object inputPassword = doc.all.item("passwd", 0);
                        HTMLInputElement elementPassword = (HTMLInputElement)inputPassword; //Move this line

                        if (inputPassword == null)
                            inputPassword = doc.all.item("password", 0);

                        if (inputPassword == null)
                            inputPassword = doc.all.item("Password", 0);

                        if (inputPassword == null)
                            inputPassword = doc.all.item("PassWord", 0);

    //Here...
                        HTMLInputElement elementPassword = (HTMLInputElement)inputPassword;
                        elementPassword.value = password;

     

    Yours Markku

    Sunday, October 08, 2006 8:41 PM
  • Well what else do we expect?? It works perfectly fine. Thanks a TON.

    But now please answer two more queries.
    First of all, why does it work? Does object inputPassword gets permanently upcasted to HTMLInputElement in the original code?

    Secondly, can you tell me if this is the best way of going about the task? I know in nearly all the web pages the name of the InputElement would be one of these. But what  if the required InputElement is in any other frame? Then, what is the best way of doing this ? How do actual password managers in a browser fill the usernames and passwords automatically.


    Sunday, October 08, 2006 9:02 PM
  • Hi

    Sorry, but I can give some kind answer only on your first question. Sorry my badly english, I hope you understant what I try to explain

    This is part of your original code.

                        Object inputPassword = doc.all.item("passwd", 0); // inputPassword is null or reference to object name "passwrd"
                        HTMLInputElement elementPassword = (HTMLInputElement)inputPassword; // elementPassword is same value as inputPassword (null or reference to "passwrd")

                        if (inputPassword == null) // if inputPassword is null (elementPassword is null, too)...
                            inputPassword = doc.all.item("password", 0); // inputPassword is null or reference to object name "password", but elementPassword is null.

                       // ...

    So, your original code find all items ("passwd", "password" and "PassWord") but elementPassword have only two value (null or reference to "passwd").

     

    Yours Markku

    Sunday, October 08, 2006 10:01 PM
  • Oh yea. I get it now. Many many thanks.  And don't bother about the english part. We all speak one language --> 0110 .

    And is there anyone else who can guide me here about the other two queries.

    1) How do i fill in usernames and passwords in other Frames.

    2) How do password managers of browsers fill in the usernames and passwords automatically. What approach do they use?
    Sunday, October 08, 2006 10:08 PM
  • I just thing that you could scan all items and check HTMLInputElement.type == "password".

    I'm not sure but this might be one way to find those user passwords. Code would be somthing like this:

    foreach (object obj in doc.all)
    {
        if (obj is HTMLInputElement)
        {
            HTMLInputElementClass btn = (HTMLInputElementClass) obj;
            if (btn.type != null)
            {
                string type = btn.type.ToLower();
                if (type == "password")
                { .... OK .... }
            }
        }
    }

    ( I find this code from http://www.codeproject.com/csharp/ie_advanced_toolbar.asp )

    Yours Markku

    Sunday, October 08, 2006 10:44 PM
  • Okay, well i don't know if it would work on different frames, but definately a better method than using names of the boxes. But, what do we do about usernames.

    Worth a shot.Thanks a ton, Markku

    But I would be interested in a sureshot way. For, in my application it is of prime importance that the usernames and passwords are inserted in 99.99% of the times.

    Any suggestions anyone.

    Monday, October 09, 2006 9:34 PM