Missing Feature: Connect-PnPonline using X509Certificate2 object for azure ad app certificate
Missing Feature
Would it be possible to add a new login method using Connect-PnpOnline which uses an already available X509Certificate2 certficate (e.g. in memory)? There are already some login methods using certificates but they always use a local certificate.
It is already possible with an in memory certficate to connect to SPO in PowerShell using pure CSOM and the OfficeDevPnP.Core.AuthenticationManager class:
$siteUrl = "https://tenantidentifier.sharepoint.com/sites/test"
$clientId = "00000000-0000-0000-0000-000000000000"
$tenantId = "00000000-0000-0000-0000-000000000000"
$certificateName = "MyCertificateName"
$keyVaultName = "MyVaultName"
#Get certificate from Azure Vault in memory
Connect-AzureRmAccount
$certificateSecret = Get-AzureKeyVaultSecret -VaultName $keyVaultName -Name $certificateName
$certBytes = [System.Convert]::FromBase64String($certificateSecret.SecretValueText)
$certificate = New-Object Security.Cryptography.X509Certificates.X509Certificate2
$certificate.Import($certBytes)
#Connect to SharePoint using AuthenticationManager class
$authManager = new-object OfficeDevPnP.Core.AuthenticationManager
$clientContext = $authManager.GetAzureADAppOnlyAuthenticatedContext($siteUrl, $clientId, $tenantId, $certificate)
# do some SharePoint stuff
$clientContext.Load($clientContext.Web)
$clientContext.ExecuteQuery()
Write-Host $clientContext.Web.Title
Expected behavior
A new login method could look like this:
Connect-PnPOnline
-Url <String>
-ClientId <String>
-Tenant <String>
-AppCertificate <X509Certificate2>
Code example:
$siteUrl = "https://tenantidentifier.sharepoint.com/sites/test"
$clientId = "00000000-0000-0000-0000-000000000000"
$tenantId = "00000000-0000-0000-0000-000000000000"
$certificateName = "MyCertificateName"
$keyVaultName = "MyVaultName"
#Get certificate from Azure Vault in memory
Connect-AzureRmAccount
$certificateSecret = Get-AzureKeyVaultSecret -VaultName $keyVaultName -Name $certificateName
$certBytes = [System.Convert]::FromBase64String($certificateSecret.SecretValueText)
$certificate = New-Object Security.Cryptography.X509Certificates.X509Certificate2
$certificate.Import($certBytes)
#Connect using PNP
Connect-PnPOnline -Url $siteUrl -ClientId $clientId -Tenant $tenantId -AppCertificate $certificate
# do some SharePoint stuff
$web = Get-PnPWeb
Write-Host $web.Title
Which version of the PnP-PowerShell Cmdlets are you using?
- [ ] PnP PowerShell for SharePoint 2013
- [ ] PnP PowerShell for SharePoint 2016
- [x] PnP PowerShell for SharePoint Online
Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.
This is definitely needed, especially for Azure Pipelines.
Sometimes it's useful to check again if something is implemented:
Here it is:
Connect-PnPOnline -Url https://contoso.sharepoint.com -ClientId '<id>' -Tenant 'contoso.onmicrosoft.com' -Certificate <X509Certificate2>
Example 23 from here: https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/connect-pnponline?view=sharepoint-ps