title: Is it possible to use Invoke Webrequest to login to a site that is using Azure AD B2C authentication?
body: <p>I am trying to configure my build-server to warmup my website by using a PowerShell script, that visits the website and login. The entire website is behind login.</p>
  
  <p>My website is using Azure AD B2C for authentication and has been configured using the starter guide from Microsoft: <a href="https://github.com/AzureADQuickStarts/B2C-WebApp-OpenIdConnect-DotNet" rel="nofollow noreferrer">https://github.com/AzureADQuickStarts/B2C-WebApp-OpenIdConnect-DotNet</a></p>
  
  <p>When I login through the browser everything works as expected.</p>
  
  <p>My current code for login looks like this:</p>
  
  <pre><code>$session = Get-AuthenticatedSession $DomainName $config.authenticationDetails $UserName $Password
  
  Function Get-AuthenticatedSession {
    param(
      [Parameter(Mandatory=$true,Position=0)]
      [string]$domainName,
      [Parameter(Mandatory=$true,Position=1)]
      [object]$authenticationDetails,
      [Parameter(Mandatory=$true,Position=2)]
      [string]$username,
      [Parameter(Mandatory=$true,Position=3)]
      [string]$password
    )
  
    # Login - to create web session with authorisation cookies
    $loginPage = "$domainName$($authenticationDetails.url)"
  
    Write-Host "Getting Login Page $loginPage"
  
    try{
      $login = Invoke-WebRequest $loginPage -SessionVariable webSession -TimeoutSec 600 -UseBasicParsing
    }catch{
      Write-Host "First attempt failed with $($_.Exception.Response.StatusCode.value__) , retrying"
      $login = Invoke-WebRequest $loginPage -SessionVariable webSession -TimeoutSec 600 -UseBasicParsing
    }
  
    Write-Host "Got Login Page, filling out form"
  
    $fields = @{}
    $fields["$($authenticationDetails.userNameField)"] = $username
    $fields["$($authenticationDetails.passwordField)"] = $password
  
    Write-Host "logging in"
  
    $request = Invoke-WebRequest -Uri $loginPage -WebSession $webSession -Method POST -Body $form  -TimeoutSec 600| Out-Null
  
    $webSession
  
    Write-Host "login done"
  }
  </code></pre>
  
  <p>This is my code for requesting a page behind login, which is run afterwards:</p>
  
  <pre><code>foreach ($page in $config.urls) {
      RequestPage "$DomainName$($page.url)" $session
  }
  
  Function RequestPage {
      param(
          [Parameter(Mandatory=$true,Position=0)]
          [string]$url,
          [Parameter(Mandatory=$true,Position=1)]
          [object]$webSession
      )
      Get-Date
      Write-Host "requesting $url ..."
      try { $request = Invoke-WebRequest $url -WebSession $webSession -TimeoutSec 60000 -UseBasicParsing} catch {
        $status = $_.Exception.Response.StatusCode.Value__
        if ($status -ne 200){
          Write-Host "ERROR Something went wrong while requesting $url" -foregroundcolor red
        }
      }
  
      Write-Host $request
  
      Get-Date
      Write-Host "Done"
      Write-Host ""
  }
  </code></pre>
  
  <p>The problem is that the script does not seem to login correctly. Whenever I request a page behind login, I always get the html of the login page back. Any help is much appreciated!</p>
  
  <p><em>EDIT</em> - It seems like there is no FORM element on the page - doh! The actual signin is a button-element with a javascript click event attached. The login page is hosted by Microsoft, so I have little control over the DOM elements:</p>
  
  <pre><code>&lt;div class="buttons"&gt;
              &lt;button id="next" tabindex="1"&gt;Sign in&lt;/button&gt;
            &lt;/div&gt;
  </code></pre>
  
  <p>So I guess my question is whether it is possible to trigger a button click using PowerShell? When I also want the script to run on my build server where IE might not be available</p>