Skip to content

Commit e5de246

Browse files
author
WHOIS: ldap_open failed
committed
update replace client secret
1 parent 7885a79 commit e5de246

File tree

1 file changed

+30
-101
lines changed

1 file changed

+30
-101
lines changed

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

Lines changed: 30 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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]
1414
> 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).
@@ -25,72 +25,50 @@ Removing an expired secret from ACS before you remove it from the application co
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 Office 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+
2. 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" # Login with corresponding scope. Should be tenant admin or anyone have the permission.
6242
```
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.
43+
3. Generate a new client secret with the following lines:
6944
7045
```powershell
71-
$clientId = 'client id of the add-in'
72-
```
46+
$appPrincipal = Get-MgServicePrincipal -Filter "AppId eq '$clientId'" # Get principal id by AppId
47+
48+
$params = @{
49+
PasswordCredential = @{
50+
DisplayName = "NewSecret" # Replace with a firendly name.
51+
}
52+
}
7353
74-
1. Generate a new client secret with the following lines:
54+
$result = Add-MgServicePrincipalPassword -ServicePrincipalId $appPrincipal.Id -BodyParameter $params # Update the secret
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.SecretText # Print the new secret
57+
$result.EndDateTime # Print the end date.
8858
```
8959
90-
1. The new client secret appears on the Windows PowerShell console. Copy it to a text file. You use it in the next procedure.
60+
4. The new client secret appears on the Windows PowerShell console. Copy it to a text file. You use it in the next procedure.
9161
9262
> [!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.
63+
> By default, the secret lasts two years if you didn't specifiy the EndDateTime. You can customize by leveraging the example below to specify the EndDateTime.
64+
> ``` powershell
65+
> $params = @{
66+
> PasswordCredential = @{
67+
> DisplayName = "NewSecret" # Replace with a firendly name.
68+
> EndDateTime = "2025-01-01T00:00:00Z" # Optional. Specify the end date you want. Using ISO 8601 format and is always in UTC time.
69+
> }
70+
> }
71+
> ```
9472
9573
## Update the remote web application in Visual Studio to use the new secret
9674
@@ -133,55 +111,6 @@ Ensure the following before you begin:
133111
1. If you changed to a new TokenHelper file, rebuild the project.
134112
1. Republish the web application.
135113
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-
185114
## See also
186115
187116
- [Provider Hosted App fails on SPO](/archive/blogs/sharepointdevelopersupport/provider-hosted-app-fails-on-spo)

0 commit comments

Comments
 (0)