PnP-PowerShell icon indicating copy to clipboard operation
PnP-PowerShell copied to clipboard

Missing Feature: Connect-PnPonline using X509Certificate2 object for azure ad app certificate

Open MrTantum opened this issue 7 years ago • 3 comments

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

MrTantum avatar Jul 17 '19 08:07 MrTantum

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

ghost avatar Jul 17 '19 08:07 ghost

This is definitely needed, especially for Azure Pipelines.

devmanlab avatar Jul 22 '19 20:07 devmanlab

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

MrTantum avatar Feb 20 '20 12:02 MrTantum