Skip to content

Commit 9e99b7c

Browse files
authored
Update replace-an-expiring-client-secret-in-a-sharepoint-add-in.md
Since Azure AD PowerShell was deprecated on March 30, 2024 (https://techcommunity.microsoft.com/t5/microsoft-entra-blog/important-azure-ad-graph-retirement-and-powershell-module/ba-p/3848270), we need to change the sample scripts to Microsoft Graph PowerShell SDK. The commit 020d3e0 (SharePoint@020d3e0) changed the scripts from Microsoft Graph PowerShell SDK to Auzre AD PowerShell on Sep 13, 2023. I've reverted this again.
1 parent 21f420b commit 9e99b7c

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

docs/sp-add-ins/replace-an-expiring-client-secret-in-a-sharepoint-add-in.md

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Replace an expiring client secret in a SharePoint Add-in
33
description: Add a new client secret for a SharePoint Add-in that is registered with AppRegNew.aspx.
4-
ms.date: 09/26/2023
4+
ms.date: 09/09/2024
55
ms.localizationpriority: high
66
ms.service: sharepoint
77
---
@@ -27,8 +27,8 @@ Removing an expired secret from ACS before you remove it from the application co
2727

2828
Ensure the following before you begin:
2929

30-
- You have installed Azure Active Directory PowerShell 2.0: [Install Azure Active Directory PowerShell for Graph](/powershell/azure/active-directory/install-adv2)
31-
- You're a tenant administrator for the Microsoft 365 tenant where the add-in was registered with the **AppRegNew.aspx** page.
30+
- You have installed Microsoft Graph Powershell SDK: [Install the Microsoft Graph PowerShell SDK](/powershell/microsoftgraph/installation)
31+
- You're a tenant administrator (or having `Application.ReadWrite.All` permission) for the Microsoft 365 tenant where the add-in was registered with the **AppRegNew.aspx** page.
3232

3333
## Generate a new secret
3434

@@ -38,35 +38,60 @@ Ensure the following before you begin:
3838
$clientId = 'client id of the add-in'
3939
```
4040
41-
1. Connect to AzureAD PowerShell.
41+
2. Connect to graph with `Application.ReadWrite.All, Directory.ReadWrite.All` scope.
4242
4343
```powershell
44-
$AzureAdCred = Get-Credential
45-
Connect-AzureAD -Credential $AzureAdCred # Login to AzureAD
44+
Connect-MgGraph -Scopes "Application.ReadWrite.All,Directory.ReadWrite.All" # Login with corresponding scope. Should be tenant admin or anyone have the permission.
4645
```
4746
48-
1. Generate a new client secret with the following lines:
47+
3. Generate a new client secret with the following lines:
4948
5049
```powershell
51-
$endDate = (Get-Date).AddYears(1)
52-
$app = Get-AzureADServicePrincipal -Filter "AppId eq '$clientId'"
53-
$objectId = $app.ObjectId
54-
55-
$base64secret = New-AzureADServicePrincipalPasswordCredential -ObjectId $objectId -EndDate $endDate
56-
New-AzureADServicePrincipalKeyCredential -ObjectId $objectId -EndDate $endDate -Type Symmetric -Usage Verify -Value $base64secret.Value
57-
New-AzureADServicePrincipalKeyCredential -ObjectId $objectId -EndDate $endDate -Type Symmetric -Usage Sign -Value $base64secret.Value
58-
59-
[Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($base64secret.Value))
60-
$base64secret.EndDate # Print the end date.
50+
$appPrincipal = Get-MgServicePrincipal -Filter "AppId eq '$clientId'" # Get principal id by AppId
51+
$params = @{
52+
PasswordCredential = @{
53+
DisplayName = "NewSecret" # Replace with a friendly name.
54+
}
55+
}
56+
$result = Add-MgServicePrincipalPassword -ServicePrincipalId $appPrincipal.Id -BodyParameter $params # Update the secret
57+
$base64Secret = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($result.SecretText)) # Convert to base64 string.
58+
$app = Get-MgServicePrincipal -ServicePrincipalId $appPrincipal.Id # get existing app information
59+
$existingKeyCredentials = $app.KeyCredentials # read existing credentials
60+
$dtStart = [System.DateTime]::Now # Start date
61+
$dtEnd = $dtStart.AddYears(2) # End date (equals to secret end date)
62+
$keyCredentials = @( # construct keys
63+
@{
64+
Type = "Symmetric"
65+
Usage = "Verify"
66+
Key = [System.Text.Encoding]::ASCII.GetBytes($result.SecretText)
67+
StartDateTime = $dtStart
68+
EndDateTIme = $dtEnd
69+
},
70+
@{
71+
type = "Symmetric"
72+
usage = "Sign"
73+
key = [System.Text.Encoding]::ASCII.GetBytes($result.SecretText)
74+
StartDateTime = $dtStart
75+
EndDateTIme = $dtEnd
76+
}
77+
) + $existingKeyCredentials # combine with existing
78+
Update-MgServicePrincipal -ServicePrincipalId $appPrincipal.Id -KeyCredentials $keyCredentials # Update keys
79+
$base64Secret # Print base64 secret
80+
$result.EndDateTime # Print the end date.
6181
```
6282
63-
1. The new client secret appears on the Windows PowerShell console. Copy it to a text file. You use it in the next procedure.
83+
4. The new client secret appears on the Windows PowerShell console. Copy it to a text file. You use it in the next procedure.
6484
6585
> [!TIP]
66-
> By default, the secret lasts one year. You can customize by leveraging the example below to specify the EndDateTime.
86+
> By default, the secret lasts two years if you didn't specify the EndDateTime. You can customize by leveraging the example below to specify the EndDateTime.
6787
>
6888
> ``` powershell
69-
> $endDate = (Get-Date).AddYears(2) # 2 year.
89+
> $params = @{
90+
> PasswordCredential = @{
91+
> DisplayName = "NewSecret" # Replace with a firendly name.
92+
> EndDateTime = "2025-01-01T00:00:00Z" # Optional. Specify the end date you want. Using ISO 8601 format.
93+
> }
94+
> }
7095
> ```
7196
7297
## Update the remote web application in Visual Studio to use the new secret

0 commit comments

Comments
 (0)