VBA - scraping websites videos | Excel VBA Part 47.4 - Logging in to a Website with Windows Security

Posted by Andrew Gould on 03 June 2019

This video explains how to use VBA to enter Windows Security credentials when you connect to a website. You'll learn how to pass a username and password when you send an XML HTTP request. You'll also learn how to use the Windows Script Host Shell object to access the security dialog box and write a username and password using the SendKeys method.

You can download any files that you need to follow the video here.

You can increase the size of the video:

Full screen mode for YouTube

You can view the video in full screen mode as shown on the left, using the icon at the bottom right of the frame.

You can also increase the quality of the video:

Changing resolution

You can improve the resolution of the video using another icon at the bottom right of the frame. This will slow down the connection speed, but increase the display and sound quality. This icon only becomes visible when you start playing the video.

Finally, if nothing happens when you play the video, check that you're not using IE in compatibility view.

This page has 3 threads Add post
21 Nov 19 at 19:56

Hi Andrew, 

Thanks a lot for such very informative videos.

I have tried dozens of different websites but have not got the login pop-up window shown on the video . I  even created an account on wise owl and used the URL https://www.wiseowl.co.uk/user/private and also did not see the login window.  Am i missing anything?  Any website I should use to test that login code?

Thanks and looking forward to hearing from you.

 

22 Nov 19 at 09:01

Hi there, no don't worry, you haven't missed anything!  Not many websites use Windows security to validate user credentials.  The websites which do tend to be internal systems such as the one I used in this video which is Wise Owl's internal diary system.  Here's a brief description of how to configure a website to use Windows authentication https://support.microsoft.com/en-gb/help/323176/how-to-implement-windows-authentication-and-authorization-in-asp-net

I hope that helps!

25 Nov 19 at 21:57

Many thanks Andrew. Actually i noticed later that it was my IE settings to have an auto login using my current credentials. When i change the setting to prompt for username then i started seeing that login security window on the intranet site  :) .   i a m still getting an error on Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy for a reason   but its not a big matter though i believe it smth to do with my intranet site. 

Thanks again and much respect !

 

Andrew G  
26 Nov 19 at 08:42

That's great!  Pleased to hear that you're making progress!

01 Sep 19 at 14:15

Hi, Andrew,

realy appreciated for your kind reply. after study your video i'm able to login the web by using IE mode, thanks again, i will need more study on this XML mode. i study some article about using Fiddler to analyze the setrequestheader and Cookie method. 

thanks, have a good day.

02 Sep 19 at 07:26

You're very welcome, Eric.  I hope you find a solution to your problem!

28 Aug 19 at 11:14

Realy appreciated for those video, is there any way to sign in a web by using XML method.

29 Aug 19 at 07:32

Hi Eric,

Yes, we do explain how to do that in the video, or did you mean something different?

29 Aug 19 at 08:06

Thanks for reply, i did see the video, the method is that VBA can pass username and password to the web. but faild. the reason is might username field is encode by base64, i guess. below are the HTML code.

after google it, i found someone said, it has to use a fake cookie or hijact the js script. 

<input name="userName" type="text" id="userName" class="ebk3-inText" placeholder="User name" style="background-image: url(&quot;data:image/png;base64,iVBORw0&quot;); background-repeat: no-repeat; background-attachment: scroll; background-size: 16px 18px; background-position: 98% 50%; cursor: auto;">

Andrew G  
29 Aug 19 at 11:44

Hi Eric,

The video is related to logging in using basic Windows security.  From the code you've posted it looks as though you need to enter a username into an HTML element on the page.  I answered a similar question on YouTube recently and include the code below to give you an idea of how to do this:

Sub TestLogin()

    Dim ie As SHDocVw.InternetExplorerMedium
    Dim doc As MSHTML.HTMLDocument
    Dim LoginForm As MSHTML.HTMLFormElement
    Dim UserNameInput As MSHTML.HTMLInputElement
    Dim PasswordInput As MSHTML.HTMLInputElement
    
    Set ie = New SHDocVw.InternetExplorer
    
    ie.Visible = True
    ie.navigate "https://www.marketingscents.com/index?page=login-new"
    
    Do While ie.readyState <> READYSTATE_COMPLETE And ie.Busy
    Loop
    
    Set doc = ie.document
    Set LoginForm = doc.getElementById("js-login-form")
    Set UserNameInput = LoginForm.getElementsByClassName("form-control input-lg")(0)
    Set PasswordInput = LoginForm.getElementsByClassName("form-control input-lg")(1)
    
    UserNameInput.Value = "my user name"
    PasswordInput.Value = "my password"
    LoginForm.submit
    
End Sub

If the page involves javascript I believe that you need to use the browser rather than the XML approach, although I don't consider myself an expert in this at all!  If you need to simulate cookies or obtain security tokens, this is somewhat more complicated.