title: Azure Authentication With Certificate Setup
body: <p>I'm trying to set up Azure Key Vault so I can access with a certificate from my PHP application. I'm trying to follow the steps at <a href="https://azurecto.com/azure-keyvault-authenticating-with-certificates-and-reading-secrets/" rel="nofollow noreferrer">https://azurecto.com/azure-keyvault-authenticating-with-certificates-and-reading-secrets/</a>, which says you have to create an AD application, but i'm getting error messages. This is what I tried.</p>
  
  <p>A. I already have a self-signed .pfx file on my Windows machine.</p>
  
  <p>B. Because I already have a .pfx file, i change up his steps a bit. I import the .pfx file into the console with </p>
  
  <pre><code>$cert = Get-PfxCertificate -FilePath "C:\azurecrt.pfx"
  </code></pre>
  
  <p>C. Then it says to create some variables</p>
  
  <pre><code>$vaultName = 'Picklistsca1'
  $dnsName = 'picklistsfakeurl.ca'
  $dummyUrl = "http://$dnsName/"
  </code></pre>
  
  <p>D. Then it says call New-AzureRmADApplication. This is where I get into trouble.</p>
  
  <pre><code>$app = New-AzureRmADApplication 
      -DisplayName $dummyUrl 
      -HomePage $dummyUrl 
      -IdentifierUris $dummyUrl 
      -CertValue $cert 
      -StartDate '2018-04-07 6:40:23 PM' 
      -EndDate '2019-04-07 6:40:23 PM'
  </code></pre>
  
  <p>I get the error message "New-AzureRmADApplication : Cannot convert a primitive value to the expected type 'Edm.Binary'. See the inner exception for more details."</p>
  
  <p>I think this is because the $cert has to be in base64 format, but everything I've tried to convert it to base64 fails. For example I've tried </p>
  
  <pre><code>$bytes = [System.IO.File]::ReadAllBytes("C:\azurecrt.pfx")
  $b64 = [System.Convert]::ToBase64String($bytes)
  </code></pre>
  
  <p>Then replace $cert with $b64 in New-AzureRmADApplication. That gives me the error "New-AzureRmADApplication : Invalid certificate: Key value is invalid certificate"</p>
  
  <p>Any advice would be greatly appreciated. Thanks</p>