Call UserForm to Re-enter Password when Error logging in

  • I've written code to gain access to a secured site with my username and password via a UserForm (for an added security feature), and it works perfectly! My question to you guys is...


    If the username or password are entered incorrectly in the UserForm and brings back a sign on error webpage, how can I re-call the UserForm to re-enter the required information?



    Here is the part of the code regarding the IE navigating and signing on:


    The only change on the webpage is that it returns the original webpage with the following sign-on error:


    <span style="font-weight: bold; color: red;">Sign In Error:</span>



    Thank you!
    Hilary

  • Re: Call UserForm to Re-enter Password when Error logging in


    will the URL in the address bar not be different if access is denied? If so you could check the URL to see if the login worked, if not - then call the userform again.


    You could wrap this whole sub in a Loop, here's a crude example:


    Code
    1. Do
    2. UserForm1.Show
    3. '//Existing code here
    4. Loop While Not IE.LocationURL = "https://access.granted.myurl.com/"


    Also, for future reference, please remove any format tags from text before wrapping it in CODE tags as this will cause it to display incorrectly. (Like your final comment block)

  • Re: Call UserForm to Re-enter Password when Error logging in


    Quote from S O;728529

    will the URL in the address bar not be different if access is denied? If so you could check the URL to see if the login worked, if not - then call the userform again.


    Thank you for your reply, S O!


    No, the URL is the same whether you are at the main page, denied access, or even if access is granted.
    I just tried various methods with your code, but I can't get it to work. Is there a way to look for script on the page?

  • Re: Call UserForm to Re-enter Password when Error logging in


    Quote from S O;728594

    It might be that it displays the result in an iFrame or something then - can you provide the link to the page? I would have to see what gets returned after the form is submitted.


    When you say "look for script"... do you mean HTML source code / javascript / PHP ???



    Unfortunately, it's an internal website that's firewalled.


    The source code is written with CSS. The sign in error is associated with the following properties.
    span


    • id: ""
    • innerHTML: "Sign In Error:"
    • innerText: "Sign In Error:"
    • isContentEditable: false
    • lang: ""
    • lastChild: text
    • lastElementChild: null
    • localName: "span"
    • namespaceURI: "http://www.w3.org/1999/xhtml"
    • nextElementSibling: null
    • nextSibling: text
    • nodeName: "SPAN"
    • nodeType: 1
    • nodeValue: null
    • offsetHeight: 15
    • offsetLeft: 146
    • offsetParent: td
    • offsetTop: 12
    • offsetWidth: 86
    • onabort: null
    • onautocomplete: null
    • outerHTML: "<span style="font-weight: bold; color: red;">Sign In Error:</span>"
    • outerText: "Sign In Error:"
    • ownerDocument: document
    • parentElement: div
    • parentNode: div
    • prefix: null
    • previousElementSibling: img
    • previousSibling: text
    • scrollHeight: 0
    • scrollLeft: 0
    • scrollTop: 0
    • scrollWidth: 0
    • shadowRoot: null
    • spellcheck: true
    • style: CSSStyleDeclaration
    • tabIndex: -1
    • tagName: "SPAN"
    • textContent: "Sign In Error:"
    • title: ""
    • translate: true
    • webkitdropzone: ""



    I try to call the error with the following, but it brings back the error that the Object doesn't support this property or method, calling back the second line there:

    Code
    1. Dim strTD As String
    2. strTD = doc.getElementByTagName("Span").innerText
    3. MsgBox strTD


    I've selected the IE and HTML references, but now I don't know what else it could be.

  • Re: Call UserForm to Re-enter Password when Error logging in


    FYI - CSS is just for styling HTML code, but that tells me that we're working with HTML source code which is much easier ;)


    The <span> tag is a generic tag, it doesn't have any individual identity so to speak, there could be 100 <span> tags in a page's source code and so trying to get this element is asking for a mistake to happen.


    It sounds like we're working with a PHP document rather than a HTML document, which makes things a bit trickier but the following might work for getting you past the login page.


  • Re: Call UserForm to Re-enter Password when Error logging in


    Oh, it works perfectly!! The only thing I had to change was move the UserForm after 'Do' to a second line.


    I'm still a novice when it comes to coding, so thank you for all your help! :)

  • Re: Call UserForm to Re-enter Password when Error logging in


    Another question for you...


    Once I'm inside the webpage, my goal is to download an excel spreadsheet. The new URL link automatically downloads the file on my desktop. Is there a way to change the name and path before it's saved?

  • Re: Call UserForm to Re-enter Password when Error logging in


    You might actually be able to use this directly without logging in if you provide your username and password:


  • Re: Call UserForm to Re-enter Password when Error logging in


    Quote from S O;728627

    You might actually be able to use this directly without logging in if you provide your username and password


    There will be multiple users using this workbook, so the UserForm is so each one can log in separately. I'll try adjusting my code to see if I can get parts of your code to work for my needs.


    Thanks!

  • Re: Call UserForm to Re-enter Password when Error logging in


    Just take the username/password section out and use it after you've logged in then:

  • Re: Call UserForm to Re-enter Password when Error logging in


    I tried your code, but there was an issue with the saved file being the information from a pop-up window that's automatically opened and closed before I get to the login screen. I'm not sure how to get around that.


    But by taking out the username and password, the user would have to enter it directly into the webpage?

  • Re: Call UserForm to Re-enter Password when Error logging in


    No, when the login page loads, it creates a pop-up window that closes itself before it finishes loading. Then once you log in, you can search on a second page that will allow you to download the results to Excel. There are two different searches that I actually want to download the results from, and the URL's for the two searches/spreadsheets are different. When the file tries to automatically download, it spits it out with the same name, which is another reason for trying to change the name.

  • Re: Call UserForm to Re-enter Password when Error logging in


    After a lot of research and trial and error, I finally got my code to work!


  • Hi all,


    The code I had posted previously worked perfectly for a few years, until recently when the host page was just updated a couple weeks ago. Now I believe my authentication line doesn't work anymore. I've tried everything I can think of, but nothing gets me logged in. It's been a few years, so I can't recall where I got the line and how it was used, and sadly, I didn't explicitly say what I used in my comments in the code.


    My question:


    Code
    1. strAuthenticate = "username=" & Username & "&password=" & Password & "&countryLocation=US" & "&submit=+working...+" & "&jsUTCOffset=420" '&jsUTCOffset=420 sets the time zone for my location.


    This is my old working code. Are the "username" and "password" ID tags? Or how do they get called in the html? I've tried the same username and password, and it doesn't work. I'd like to mention that the host page now has a log-in pop-up form.


    Code
    1. strAuthenticate = "username=" & Username & "&password=" & Password & "&onclick=return validateAndSubmit(4768);"


    This is my latest version, but I don't think I'm calling the form, or calling the submit button correctly. Here is the html for the submit button:


    HTML
    1. <button class="btn btn-primary hid-loginPanel-btn2" type="submit" id="hidSignIn" onclick="return validateAndSubmit(4768);"><span class="hid-social-text"><span>Sign In</span></span></button>



    Can anyone advise please?


    Thank you,


    Hilary

  • Hi Hilary,


    In your original code the variables were being passed to the server via what's called a "GET" request, whereby the variables are displayed as part of the URL and the server picks them up from there.


    This is probably the most insecure way of passing data to a server as your details are essentially in plain sight for anyone to steal. I would like to think that part of the update involved them changing the way data is sent to a "POST" method after the data has been securely encrypted/hashed.


    If this is the case, then your code will no longer work. You will have to check the page's source code to see how the login is authenticated (I can see it's a javascript function, so probably an AJAX call somewhere) and then you will have to re-write your code to accomodate.


    Sorry it's not better news!


    regards,


    Sam

  • No, it has always been a POST request. Bottom of page 1 has my working code.


    Code
    1. WHTTP.Open "POST", mainURL, False
    2. WHTTP.setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
    3. WHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36"
    4. WHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    5. WHTTP.send strAuthenticate


    It is still a POST request, but I don't know how to call the the username and password. I believe it's just the ID? And then submit the authentication code.

  • Do you mean how to set the username and password for the request?


    [VBA]WHTTP.setCredentials username, password, 0[/VBA]


    Other than that, it could be that the parameter names have been updated on the server-side code which unfortunately you will have no way of knowing unless you are comfortable using F12 developer tools to capture the network response or ask the developer(s) that updated the page.