Generate & Replace an expiring client secret in a SharePoint Add-in
Hi,
I have created a provider hosted app for SharePoint. It got expired due to validity of secrete because usually client secret validity is one year. Now the app started throwing exception to me i.e. ""Invalid JWT token. Could not resolve issuer token."
I did couple of research on it & found the solution from site "https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/replace-an-expiring-client-secret-in-a-sharepoint-add-in".
I have made some changes in this script to make little bit easier to use .
Here is the script :
#Actual Article
#https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/replace-an-expiring-client-secret-in-a-sharepoint-add-in
#MSOnline
#Get help from : https://www.powershellgallery.com/packages/MSOnline/1.1.166.0
#Inspect
#PS> Save-Module -Name MSOnline -Path <path>
#Install
#PS> Install-Module -Name MSOnline
cls
#Get app details
Connect-MsolService
$clientID = “<<YourClientIdGuidHere>>”
Write-Host "-------------------------------------------------`n" -ForegroundColor Yellow
Write-Host "Get the list of Apps :`n" -ForegroundColor Yellow
Get-MsolServicePrincipal -AppPrincipalId $clientID
#Get-MsolServicePrincipalCredential -AppPrincipalId $clientID -ReturnKeyValues $false | Where-Object { ($_.Type -ne “Other”) -and ($_.Type -ne “Asymmetric”) }
$clientKeys=Get-MsolServicePrincipalCredential -AppPrincipalId $clientID -ReturnKeyValues $false | Where-Object { ($_.Type -ne “Other”) -and ($_.Type -ne “Asymmetric”) }
$clientKeys
Write-Host "=========================== END : Get the list of Apps ===========================`n" -ForegroundColor Green
# Get Keys from app
Get-MsolServicePrincipal -AppPrincipalId $clientID
$keys = Get-MsolServicePrincipalCredential -AppPrincipalId $clientID -ReturnKeyValues $true
Write-Host "-------------------------------------------------`n" -ForegroundColor Yellow
Write-Host "Showing the list of keys for the App :`n" -ForegroundColor Yellow
$keys
$key1=$clientKeys[0].KeyId.ToString()
$key2=$clientKeys[1].KeyId.ToString()
$key3=$clientKeys[2].KeyId.ToString()
Remove-MsolServicePrincipalCredential -KeyIds @($key1," $key2"," $key3") -AppPrincipalId $clientId
Write-Host "=========================== END : Showing the list of keys for the App ===========================`n" -ForegroundColor Green
#Create new screte for the app
Write-Host "`n-------------------------------------------------`n" -ForegroundColor Yellow
Write-Host "Create new secret for the app :`n" -ForegroundColor Yellow
$bytes = New-Object Byte[] 32
$rand = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rand.GetBytes($bytes)
$rand.Dispose()
$newClientSecret = [System.Convert]::ToBase64String($bytes)
Write-Host "New client secret is : ($newClientSecret) `n" -ForegroundColor Red
$dtStart = [System.DateTime]::Now
$dtEnd = $dtStart.AddYears(3)
New-MsolServicePrincipalCredential -AppPrincipalId $clientID -Type Symmetric -Usage Sign -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
New-MsolServicePrincipalCredential -AppPrincipalId $clientID -Type Symmetric -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
New-MsolServicePrincipalCredential -AppPrincipalId $clientID -Type Password -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
Write-Host "New client secret is : ($newClientSecret) `n" -ForegroundColor Red
#$newClientSecret
Write-Host "=========================== END : Create new secret for the app ===========================`n" -ForegroundColor Green
I have created a provider hosted app for SharePoint. It got expired due to validity of secrete because usually client secret validity is one year. Now the app started throwing exception to me i.e. ""Invalid JWT token. Could not resolve issuer token."
I did couple of research on it & found the solution from site "https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/replace-an-expiring-client-secret-in-a-sharepoint-add-in".
I have made some changes in this script to make little bit easier to use .
Here is the script :
#Actual Article
#https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/replace-an-expiring-client-secret-in-a-sharepoint-add-in
#MSOnline
#Get help from : https://www.powershellgallery.com/packages/MSOnline/1.1.166.0
#Inspect
#PS> Save-Module -Name MSOnline -Path <path>
#Install
#PS> Install-Module -Name MSOnline
cls
#Get app details
Connect-MsolService
$clientID = “<<YourClientIdGuidHere>>”
Write-Host "-------------------------------------------------`n" -ForegroundColor Yellow
Write-Host "Get the list of Apps :`n" -ForegroundColor Yellow
Get-MsolServicePrincipal -AppPrincipalId $clientID
#Get-MsolServicePrincipalCredential -AppPrincipalId $clientID -ReturnKeyValues $false | Where-Object { ($_.Type -ne “Other”) -and ($_.Type -ne “Asymmetric”) }
$clientKeys=Get-MsolServicePrincipalCredential -AppPrincipalId $clientID -ReturnKeyValues $false | Where-Object { ($_.Type -ne “Other”) -and ($_.Type -ne “Asymmetric”) }
$clientKeys
Write-Host "=========================== END : Get the list of Apps ===========================`n" -ForegroundColor Green
# Get Keys from app
Get-MsolServicePrincipal -AppPrincipalId $clientID
$keys = Get-MsolServicePrincipalCredential -AppPrincipalId $clientID -ReturnKeyValues $true
Write-Host "-------------------------------------------------`n" -ForegroundColor Yellow
Write-Host "Showing the list of keys for the App :`n" -ForegroundColor Yellow
$keys
$key1=$clientKeys[0].KeyId.ToString()
$key2=$clientKeys[1].KeyId.ToString()
$key3=$clientKeys[2].KeyId.ToString()
Remove-MsolServicePrincipalCredential -KeyIds @($key1," $key2"," $key3") -AppPrincipalId $clientId
Write-Host "=========================== END : Showing the list of keys for the App ===========================`n" -ForegroundColor Green
#Create new screte for the app
Write-Host "`n-------------------------------------------------`n" -ForegroundColor Yellow
Write-Host "Create new secret for the app :`n" -ForegroundColor Yellow
$bytes = New-Object Byte[] 32
$rand = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rand.GetBytes($bytes)
$rand.Dispose()
$newClientSecret = [System.Convert]::ToBase64String($bytes)
Write-Host "New client secret is : ($newClientSecret) `n" -ForegroundColor Red
$dtStart = [System.DateTime]::Now
$dtEnd = $dtStart.AddYears(3)
New-MsolServicePrincipalCredential -AppPrincipalId $clientID -Type Symmetric -Usage Sign -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
New-MsolServicePrincipalCredential -AppPrincipalId $clientID -Type Symmetric -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
New-MsolServicePrincipalCredential -AppPrincipalId $clientID -Type Password -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
Write-Host "New client secret is : ($newClientSecret) `n" -ForegroundColor Red
#$newClientSecret
Write-Host "=========================== END : Create new secret for the app ===========================`n" -ForegroundColor Green
Comments