Skip to content

Commit a3a1076

Browse files
Merge pull request SharePoint#9027 from qianghuang94/main
update replace client secret
2 parents 5f2912c + a4523c5 commit a3a1076

File tree

1 file changed

+43
-111
lines changed

1 file changed

+43
-111
lines changed

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

Lines changed: 43 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,103 @@
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: 06/13/2022
4+
ms.date: 06/21/2023
55
ms.prod: sharepoint
66
ms.localizationpriority: high
77
---
88

99
# Replace an expiring client secret in a SharePoint Add-in
1010

11-
Client secrets for SharePoint Add-ins that are registered by using the AppRegNew.aspx page expire after one year. This article explains how to add a new secret for the add-in, and how to create a new client secret that is valid for three years.
11+
Client secrets for SharePoint Add-ins that are registered by using the **AppRegNew.aspx** page expire after one year. This article explains how to add a new secret for the add-in, and how to create a new client secret that is valid for a customized date.
1212

1313
> [!NOTE]
14-
> This article is about SharePoint Add-ins that are distributed through an organization catalog and registered with the AppRegNew.aspx page. If the add-in is registered on the Seller Dashboard, see [Create or update client IDs and secrets in the Seller Dashboard](/office/dev/store/create-or-update-client-ids-and-secrets).
14+
> This article is about SharePoint Add-ins that are distributed through an organization catalog and registered with the **AppRegNew.aspx** page. If the add-in is registered on the Seller Dashboard, see [Create or update client IDs and secrets in the Seller Dashboard](/office/dev/store/create-or-update-client-ids-and-secrets).
1515
1616
## Recommended maintenance schedule
1717

18-
We recommend to create new secrets a minimum of 30 days before they expire. This gives you a month of time before the old credentials expire.
18+
We recommend creating new secrets a minimum of 30 days before they expire. This gives you a month of time before the old credentials expire.
1919

20-
We recommend to only remove secrets a minimum of 7 days after expiration, provided you have removed them from the application configuration.
20+
We recommend only removing secrets a minimum of 7 days after expiration, provided you have removed them from the application configuration.
2121

2222
Removing an expired secret from ACS before you remove it from the application configuration will cause errors.
2323

2424
## Prerequisites
2525

2626
Ensure the following before you begin:
2727

28-
- Microsoft Online Services Sign-In Assistant is installed on the development computer.
29-
- You can connect to Office 365 with PowerShell: [Connect to Office 365 PowerShell](/office365/enterprise/powershell/connect-to-office-365-powershell)
30-
- You're a tenant administrator for the Office 365 tenant (or a farm administrator on the farm) where the add-in was registered with the AppRegNew.aspx page.
28+
- You have installed Microsoft Graph Powershell SDK: [Install the Microsoft Graph PowerShell SDK](/powershell/microsoftgraph/installation)
29+
- 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.
3130

32-
## Find out the expiration dates of the SharePoint Add-ins installed to the Office 365 tenancy
31+
## Generate a new secret
3332

34-
1. Open Windows PowerShell and run the following cmdlet:
33+
1. Create a client ID variable with the following line, using the client ID of the SharePoint Add-in as the parameter:
3534

3635
```powershell
37-
Connect-MsolService
36+
$clientId = 'client id of the add-in'
3837
```
39-
40-
1. At the sign-in prompt, enter tenant-administrator (or farm administrator) credentials for the Office 365 tenancy or farm where the add-in was registered with AppRegNew.aspx.
41-
1. Generate a report that lists each add-in and the date that its secret expires with the following lines. Note the following about this code:
42-
43-
- It first filters out Microsoft's own applications, add-ins still under development (and a now-deprecated type of add-in that was called autohosted).
44-
- From the remainder, it filters out non-SharePoint add-ins and add-ins that use asymmetric keys, such as workflows.
38+
1. Connect to graph with **Application.ReadWrite.All** scope:
4539
4640
```powershell
47-
$applist = Get-MsolServicePrincipal -all |Where-Object -FilterScript { ($_.DisplayName -notlike "*Microsoft*") -and ($_.DisplayName -notlike "autohost*") -and ($_.ServicePrincipalNames -notlike "*localhost*") }
48-
49-
foreach ($appentry in $applist) {
50-
$principalId = $appentry.AppPrincipalId
51-
52-
Get-MsolServicePrincipalCredential -AppPrincipalId $principalId -ReturnKeyValues $false | Where-Object { $_.Type -eq "Password" } | ForEach-Object {
53-
[PSCustomObject][Ordered]@{
54-
PrincipalName = $appentry.DisplayName
55-
PrincipalId = $principalId
56-
KeyID = $_.KeyId
57-
StartDate = $_.StartDate
58-
EndDate = $_.EndDate
59-
} | Export-Csv -Path C:\temp\appsec.csv -NoTypeInformation -Delimiter ';' -Append
60-
}
61-
}
41+
Connect-MgGraph -Scopes "Application.ReadWrite.All"
42+
# login with the corresponding scope; this user should be a tenant admin or anyone granted this permission
6243
```
63-
64-
1. Open the file C:\temp\appsec.csv to see the report. Leave the Windows PowerShell window open for the next procedure, if any of the secrets are near expiration.
65-
66-
## Generate a new secret
67-
68-
1. Create a client ID variable with the following line, using the client ID of the SharePoint Add-in as the parameter.
44+
45+
1. Generate a new client secret with the following lines:
6946
7047
```powershell
71-
$clientId = 'client id of the add-in'
72-
```
48+
$appPrincipal = Get-MgServicePrincipal -Filter "AppId eq '$clientId'" # Get principal id by AppId
7349
74-
1. Generate a new client secret with the following lines:
50+
$params = @{
51+
PasswordCredential = @{
52+
DisplayName = "NewSecret" # Replace with a friendly name.
53+
}
54+
}
7555
76-
```powershell
77-
$bytes = New-Object Byte[] 32
78-
$rand = [System.Security.Cryptography.RandomNumberGenerator]::Create()
79-
$rand.GetBytes($bytes)
80-
$rand.Dispose()
81-
$newClientSecret = [System.Convert]::ToBase64String($bytes)
82-
$dtStart = [System.DateTime]::Now
83-
$dtEnd = $dtStart.AddYears(1)
84-
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Symmetric -Usage Sign -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
85-
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Symmetric -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
86-
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Password -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
87-
$newClientSecret
56+
$result = Add-MgServicePrincipalPassword -ServicePrincipalId $appPrincipal.Id -BodyParameter $params # Update the secret
57+
58+
$result.SecretText # Print the new secret
59+
$result.EndDateTime # Print the end date.
8860
```
8961
9062
1. The new client secret appears on the Windows PowerShell console. Copy it to a text file. You use it in the next procedure.
9163
9264
> [!TIP]
93-
> By default, the add-in secret lasts one year. You can set this to a shorter or longer by using the **-EndDate** parameter on the three calls of the **New-MsolServicePrincipalCredential** cmdlet.
65+
> 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.
66+
>
67+
> ``` powershell
68+
> $params = @{
69+
> PasswordCredential = @{
70+
> DisplayName = "NewSecret" # Replace with a firendly name.
71+
> EndDateTime = "2025-01-01T00:00:00Z" # Optional. Specify the end date you want. Using ISO 8601 format.
72+
> }
73+
> }
74+
> ```
9475
9576
## Update the remote web application in Visual Studio to use the new secret
9677
9778
> [!IMPORTANT]
98-
> If your add-in was originally created with a pre-release version of the Microsoft Office Developer Tools for Visual Studio, it may contain an out-of-date version of the TokenHelper.cs (or .vb) file. If the file does not contain the string "secondaryClientSecret", it is out of date and must be replaced before you can update the web application with a new secret. To obtain a copy of a release version of the file, you need Visual Studio 2012 or later. Create a new SharePoint Add-in project in Visual Studio. Copy the TokenHelper file from it to the web application project of your SharePoint Add-in.
79+
> If your add-in was originally created with a pre-release version of the Microsoft Office Developer Tools for Visual Studio, it may contain an out-of-date version of the **TokenHelper.[cs|vb]** file. If the file does not contain the string `secondaryClientSecret`, it is out of date and must be replaced before you can update the web application with a new secret. To obtain a copy of a release version of the file, you need Visual Studio 2012 or later. Create a new SharePoint Add-in project in Visual Studio. Copy the **TokenHelper.[cs|vb]** file from it to the web application project of your SharePoint Add-in.
9980
100-
1. Open the SharePoint Add-in project in Visual Studio, and open the web.config file for the web application project. In the **appSettings** section, there are keys for the client ID and client secret. The following is an example:
81+
1. Open the SharePoint Add-in project in Visual Studio, and open the **web.config** file for the web application project. In the `appSettings` section, there are keys for the client ID and client secret. The following is an example:
10182
10283
```XML
10384
<appSettings>
10485
<add key="ClientId" value="your client id here" />
10586
<add key="ClientSecret" value="your old secret here" />
106-
... other settings may be here ...
87+
... other settings may be here ...
10788
</appSettings>
10889
```
10990
110-
1. Change the name of the **ClientSecret** key to `SecondaryClientSecret` as shown in the following example:
91+
1. Change the name of the `ClientSecret` key to `SecondaryClientSecret` as shown in the following example:
11192
11293
```XML
11394
<add key="SecondaryClientSecret" value="your old secret here" />
11495
```
11596
11697
> [!NOTE]
117-
> If you are performing this procedure for the first time, there is no **SecondaryClientSecret** property entry at this point in the configuration file. However, if you are performing the procedure for a subsequent client secret expiration (second or third), the property **SecondaryClientSecret** is already present and contains the initial or already expired old secret. In this case, delete the **SecondaryClientSecret** property first before renaming **ClientSecret**.
98+
> If you are performing this procedure for the first time, there is no `SecondaryClientSecret` property entry at this point in the configuration file. However, if you are performing the procedure for a subsequent client secret expiration (second or third), the property `SecondaryClientSecret` is already present and contains the initial or already expired old secret. In this case, delete the `SecondaryClientSecret` property first before renaming `ClientSecret`.
11899
119-
1. Add a new **ClientSecret** key and give it your new client secret. Your markup should now look like the following:
100+
1. Add a new `ClientSecret` key and give it your new client secret. Your markup should now look like the following:
120101
121102
```XML
122103
<appSettings>
@@ -128,60 +109,11 @@ Ensure the following before you begin:
128109
```
129110
130111
> [!IMPORTANT]
131-
> You will not be able to use the newly generated client secret until the current client secret expires. Therefore, changing the ClientId key to the new client secret without the SecondaryClientSecret key present will not work. You must follow the procedure in this article and wait for the previous client secret to expire. You can then remove the SecondaryClientSecret if you want to.
112+
> You will not be able to use the newly generated client secret until the current client secret expires. Therefore, changing the `ClientId` key to the new client secret without the `SecondaryClientSecret` key present will not work. You must follow the procedure in this article and wait for the previous client secret to expire. You can then remove the SecondaryClientSecret if you want to.
132113
133-
1. If you changed to a new TokenHelper file, rebuild the project.
114+
1. If you changed to a new **TokenHelper.[cs|vb]** file, rebuild the project.
134115
1. Republish the web application.
135116
136-
## Create a client secret that is valid for three years
137-
138-
For expired client secrets, first you must delete all of the expired secrets for a given **clientId**. You then create a new one with MSO PowerShell, wait at least 24 hours, and test the app with the new **clientId** and **ClientSecret** key.
139-
140-
1. Connect to MSOnline using the tenant admin user with the following markup using SharePoint Windows PowerShell.
141-
142-
```powershell
143-
import-module MSOnline
144-
$msolcred = get-credential
145-
connect-msolservice -credential $msolcred
146-
```
147-
148-
1. Get **ServicePrincipals** and keys. Printing **$keys** returns three records. You also see the **EndDate** of each key. Confirm whether your expired key appears there.
149-
150-
> [!NOTE]
151-
> The **clientId** needs to match your expired **clientId**. It's recommended to delete all keys, both expired and unexpired, for this **clientId**.
152-
153-
```powershell
154-
$clientId = "27c5b286-62a6-45c7-beda-abbaea6eecf2"
155-
$keys = Get-MsolServicePrincipalCredential -AppPrincipalId $clientId
156-
$keys
157-
```
158-
159-
1. Remove all keys once you have confirmed that they are indeed expired.
160-
161-
```powershell
162-
Remove-MsolServicePrincipalCredential -KeyIds $keys.KeyId -AppPrincipalId $clientId
163-
```
164-
165-
1. Generate a new **ClientSecret** for this **clientID**. It uses the same **clientId** as set in the preceding step. The new **ClientSecret** is valid for three years.
166-
167-
```powershell
168-
$bytes = New-Object Byte[] 32
169-
$rand = [System.Security.Cryptography.RandomNumberGenerator]::Create()
170-
$rand.GetBytes($bytes)
171-
$rand.Dispose()
172-
$newClientSecret = [System.Convert]::ToBase64String($bytes)
173-
$dtStart = [System.DateTime]::Now
174-
$dtEnd = $dtStart.AddYears(3)
175-
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Symmetric -Usage Sign -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
176-
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Symmetric -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
177-
New-MsolServicePrincipalCredential -AppPrincipalId $clientId -Type Password -Usage Verify -Value $newClientSecret -StartDate $dtStart -EndDate $dtEnd
178-
$newClientSecret
179-
```
180-
181-
1. Copy the output of **$newClientSecret**.
182-
1. Replace the **Web.config** with this **ClientId** and **ClientSecret**. You don't need **SecondaryClientSecret** app settings.
183-
1. Wait at least 24 hours to propagate **ClientSecret** to SharePoint Office (SPO).
184-
185117
## See also
186118
187119
- [Provider Hosted App fails on SPO](/archive/blogs/sharepointdevelopersupport/provider-hosted-app-fails-on-spo)

0 commit comments

Comments
 (0)