Outils pour utilisateurs

Outils du site


reseau:cloud:azure:configurerapppoursharepoint

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
reseau:cloud:azure:configurerapppoursharepoint [2026/04/05 22:08] – créée techer.charles_educ-valadon-limoges.frreseau:cloud:azure:configurerapppoursharepoint [2026/04/10 22:24] (Version actuelle) – [Utilisation d'un certificat] techer.charles_educ-valadon-limoges.fr
Ligne 1: Ligne 1:
-==== Infrastructure réalisée ====+====== Configurer une application enregistrée dans Entra ID pour gérer une équipe Sharepoint ======
  
-Documenso => MinIO => rclone (bidirectionnel) <=> Equipe Teams (API Graph)+===== Inscription d'une application =====
  
-<WRAP center round info> 
-La création d’applications pour Microsoft 365, pour accéder aux données et services de Microsoft 365, ou pour permettre aux utilisateurs de se connecter à l'application créée via leur compte professionnel ou scolaire, il est nécessaire d’**inscrire l’application** dans Entra ID. 
- 
-L'inscription d’application enregistre l'application auprès du Microsoft Entra ID et définit des paramètres tels que les permissions API ou les flux d’authentification. 
  
 Une inscription d’application dans Entra se fait :  Une inscription d’application dans Entra se fait : 
   * depuis le **portail Azure**,   * depuis le **portail Azure**,
   * en **CLI/script** avec l'**API Microsoft Graph** par exemple en utilisant **Powershell**.   * en **CLI/script** avec l'**API Microsoft Graph** par exemple en utilisant **Powershell**.
-</WRAP>+
  
 ===== Installer le module Microsoft.Graph de Powershell===== ===== Installer le module Microsoft.Graph de Powershell=====
Ligne 18: Ligne 14:
 ==== Installer Microsoft.Graph de Powershell ==== ==== Installer Microsoft.Graph de Powershell ====
  
- +  * prérequis : 
-    * prérequis : +
         * permissions API (Microsoft Graph) : Sites.Selected (Application)         * permissions API (Microsoft Graph) : Sites.Selected (Application)
  
-    * ouvrir une session Powershell en tant, qu'administrateur+  * ouvrir une session Powershell en tant, qu'administrateur
  
 <code> <code>
Ligne 35: Ligne 30:
     * Portail Azure => Entra ID     * Portail Azure => Entra ID
     * Inscription d'applications => Nouvelle inscription     * Inscription d'applications => Nouvelle inscription
-    * Nom : minio-S3+    * Nom : app
     * Locataire unique seulement     * Locataire unique seulement
     * S'incrire     * S'incrire
Ligne 42: Ligne 37:
     * Tenant ID     * Tenant ID
     * Client ID     * Client ID
-    * Ajouter un secret Client (dans Certificates & Secrets)+    * l'ajout du certificat se fera ultérieurement (dans Certificates & Secrets)
  
   * Ajouter la permission Microsoft Graph :   * Ajouter la permission Microsoft Graph :
     * Autorisations d'application (et non Autorisations déléguées) :      * Autorisations d'application (et non Autorisations déléguées) : 
       * Sites.Selected       * Sites.Selected
-      * Sites.FullControl.All (temporaire pour attribuer l'accès puis la retirer ) 
-      * Files.Read.All 
-      * Files.ReadWrite.All 
- 
  
 <WRAP center round> <WRAP center round>
-Les permissions **Files.Read.All** et **Files.ReadWrite.All** donnent l’accès à toutes les bibliothèques pour lesquelles l'App est autorisée, mais la permission **Sites.Selected** restreint l'accès au site explicitement autorisé via Graph.+La permission **Sites.Selected** restreint l'accès au site explicitement autorisé via Graph.
 </WRAP> </WRAP>
  
 <WRAP center round info> <WRAP center round info>
-Une autorisation d'application ne nécessite de compte utilisateur :+Une autorisation d'application ne nécessite pas de compte utilisateur :
   * L'application agit en autonomie (service, script, automatisation).   * L'application agit en autonomie (service, script, automatisation).
   * Nécessite un consentement administrateur.   * Nécessite un consentement administrateur.
 </WRAP> </WRAP>
  
-  * Grant admin consent. +  * Demander le consentement de l'administrateur.
- +
-  * Adresse email 0365 utilisée pour l’envoi  afin que l'app puisse avoir le droit d’envoyer au nom de ce compte.+
  
 ==== En CLI ==== ==== En CLI ====
Ligne 85: Ligne 74:
  
 <code> <code>
-$app = New-MgApplication -DisplayName "Minio-Sharepoint"+$app = New-MgApplication -DisplayName "Application"
 </code> </code>
  
Ligne 94: Ligne 83:
 </code> </code>
 ===== Autoriser l'app enregistrée à mettre à jour les dossiers de l'équipe Teams ===== ===== Autoriser l'app enregistrée à mettre à jour les dossiers de l'équipe Teams =====
-Cela ne peut se faire depuis l'interface d'administration Entra ID mais en utilisant Powershell.+Cela ne peut pas se faire depuis l'interface d'administration Entra ID mais en utilisant Powershell.
  
  
Ligne 103: Ligne 92:
 <code> <code>
 $cert = New-SelfSignedCertificate ` $cert = New-SelfSignedCertificate `
-  -Subject "CN=minio-s3" ` +  -Subject "CN=applicaton" ` 
-  -CertStoreLocation "Cert:\LocalMachine\My" `+  -CertStoreLocation "Cert:\CurrentUSer\My" `
   -KeySpec Signature `   -KeySpec Signature `
   -KeyUsage DigitalSignature `   -KeyUsage DigitalSignature `
-  -KeyExportPolicy NonExportable `+  -KeyExportPolicy Exportable `
   -HashAlgorithm SHA256 `   -HashAlgorithm SHA256 `
   -KeyLength 2048 `   -KeyLength 2048 `
   -NotAfter (Get-Date).AddYears(4)   -NotAfter (Get-Date).AddYears(4)
- 
-$cert.Thumbprint 
- 
- 
- 
 </code> </code>
  
-  * Notez l'empreinte du certfifcat+  * Notez l'empreinte du certificat
  
 <code> <code>
Ligne 124: Ligne 108:
 </code> </code>
  
-  * Vérifier que le certificat est BIEN présent au BON endroit dans Cert:\LocalMachine\My+  * Vérifier que le certificat est BIEN présent au BON endroit dans Cert:\CurrentUser\My
  
 <code> <code>
-Get-Item "Cert:\LocalMachine\My\<TON_THUMBPRINT>"+Get-Item "Cert:\CurrentUSer\My\<TON_THUMBPRINT>"
 </code> </code>
  
Ligne 133: Ligne 117:
  
 <code> <code>
-$cert = Get-Item "Cert:\LocalMachine\My\<THUMBPRINT>"+$cert = Get-Item "Cert:\CurrentUSer\My\<THUMBPRINT>"
 $cert.HasPrivateKey $cert.HasPrivateKey
 </code> </code>
Ligne 139: Ligne 123:
 $cert.HasPrivateKey doit renvoyer True $cert.HasPrivateKey doit renvoyer True
  
-   * exporter le certificat+=== Export du certificat PUBLIC (.cer) à importer dans Entra ID ===
  
 <code> <code>
 +Export-Certificate `
 +    -Cert "Cert:\CurrentUSer\My\$($cert.Thumbprint)" `
 +    -FilePath "application.cer"
 +</code>
  
-Export-PfxCertificate ` +  * importer le certificat dans l'application à partir du portail Azure
-  -Cert $cert ` +
-  -FilePath "$env:TEMP\minio-s3.pfx"+
-  -Password (ConvertTo-SecureString -String "TempPwd123!" -AsPlainText -Force)+
  
-</code>  +  Test avec PowerShell (certificat local)
-  +
-  importer dans user+
  
 <code> <code>
-Import-PfxCertificate ` +Connect-MgGraph `   
-  -FilePath "$env:TEMP\minio-s3.pfx" ` +   -TenantId "<TENANT_ID>" `   
-  -CertStoreLocation Cert:\CurrentUser\My ` +   -ClientId "<APP_ID>" `   
-  -Password (ConvertTo-SecureString -String "TempPwd123!-AsPlainText -Force)+   -CertificateThumbprint "<THUMBPRINT> 
 + 
 +=> Résultat attendu 
 +Plain TextWelcome To Microsoft Graph!
 </code> </code>
  
-   vérifier+  Puis :
  
 <code> <code>
-Get-ChildItem Cert:\CurrentUser\My | +Get-MgContext 
-Where-Object Thumbprint -eq "<THUMBPRINT>"+ 
 +=> on doit voir 
 + 
 +ClientId               : <id client> 
 +TenantId               : <ID tenant> 
 +Scopes                 : {Sites.Selected} 
 +AuthType               : AppOnly 
 +TokenCredentialType    : ClientCertificate 
 +CertificateThumbprint <empreinte>
 </code> </code>
  
-  * Export du certificat PUBLIC à importer dans Entra ID+=== Exporter le certificat pour obtenir un PFX PKCS#1 (clé privée incluse) === 
  
 <code> <code>
-Export-Certificate ` +$thumb = "EMPREINTE_DU_CERTIFICAT" 
-    -Cert "Cert:\LocalMachine\My\$($cert.Thumbprint)" + 
-    -FilePath "minio-s3.cer"+$cert = Get-Item "Cert:\CurrentUser\My\$thumb" 
 + 
 +Export-PfxCertificate ` 
 +  -Cert $cert ` 
 +  -FilePath "graph-auth.pfx
 +  -Password (ConvertTo-SecureString -String "MotDePasseFort123!" -AsPlainText -Force)
 </code> </code>
  
-==== Installer PowerShell 7 ====+=== Importer un PFX sur une autre machine ===
  
-==== Installer le module Microsoft.Graph ==== +<code> 
-    * prérequis :  +Import-PfxCertificate ` 
-        * permissions API (Microsoft Graph) : Sites.Selected (Application)+   -FilePath "fichier.pfx" ` 
 +   -CertStoreLocation Cert:\CurrentUser\My `   
 +   -Password (ConvertTo-SecureString -String "MotDePasseFort123!" -AsPlainText) 
 +</code>
  
-    * ouvrir une session Powershell en tant, qu'administrateur+=== Générer une clé PKCS#8 === 
 + 
 +  * Recharger le PFX 
  
 <code> <code>
-Install-Module Microsoft.Graph  +$pfxPath = "c:\temp\fichier.pfx" 
-Import-Module Microsoft.Graph+$pfxPassword = "TempPfxPassword!ChangeMe" 
 + 
 +$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2( 
 +    $pfxPath, 
 +    $pfxPassword, 
 +    [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable 
 +)
 </code> </code>
  
 + * Récupérer la clé privée RSA
  
-  * script Powershell pour se conncter t etest la création d'un dossier +<code> 
 +$rsa = $cert.PrivateKey 
 +</code>
  
-<WRAP center round important> +  * Exporter la clé AU FORMAT PKCS#8
-Il est important de se connecter en tant qu'application et non en tant qu'utilisateur. +
-</WRAP>+
  
 <code> <code>
-############################################################ +Récupère les paramètres 
-# CONFIGURATION pour l'accès avec l'application enregistrée +$params = $rsa.ExportParameters($true)
-############################################################+
  
-$TenantId     = "Tenant_educ-valadon+Crée un RSA moderne 
-$AppId        "APP_ID_DE_TON_APPLICATION"+$rsa2 [System.Security.Cryptography.RSA]::Create() 
 +$rsa2.ImportParameters($params)
  
-$Thumbprint "EMPREINTE_DU_CERTIFICAT"+# Export PKCS#8 
 +$pkcs8 $rsa2.ExportPkcs8PrivateKey()
  
-$SitePath      "/sites/Signaturesnumriques" +$base64 [System.Convert]::ToBase64String( 
-$NewFolderName = "Test-SitesSelected-Cert-OK"+    $pkcs8, 
 +    [System.Base64FormattingOptions]::InsertLineBreaks 
 +)
  
  
 +$pem = @"
 +-----BEGIN PRIVATE KEY-----
 +$base64
 +-----END PRIVATE KEY-----
 +"@
  
-$SitePath = "/sites/Signaturesnumriques" +$pem | Set-Content ` 
-$NewFolderName = "Test-SitesSelected-OK"+  -Path C:\Temp\fichier.key ` 
 +  -Encoding ASCII 
 +</code>
  
-############################################################ +=== Résumé .cer .pfx ===
-# ÉTAPE 1 – OBTENIR UN TOKEN D’APPLICATION (CLIENT CREDENTIALS) +
-############################################################+
  
-Write-Host "Authentification par certificat…"+<WRAP center round info> 
 +Un fichier .cer contient uniquement la **clé publique du certificat**. Cela permet :  
 +  * de déclarer le certificat dans Entra ID 
 +  * permet à Microsoft de vérifier les signatures
  
-$cert = Get-Item "Cert:\LocalMachine\My\$Thumbprint"+La clé privée est déjà stockée dans Windows : 
 +  * dans **Cert:\LocalMachine\My** ou **Cert:\CurrentUser\My** 
 +  * aucun PFX n’est nécessaire tant que l'on se connecte depuis le PC où se trouve la clé privée. En indiquant l'empreinte de la clé (thumbprint), cela permet de sélectionner la bonne clé privée.
  
-$assertion = [Microsoft.Identity.Client.ConfidentialClientApplicationBuilder]::Create($AppId). +Un fichier .cer ne permet pas 
-    WithTenantId($TenantId). +  * de s’authentifier 
-    WithCertificate($cert). +  * de signer
-    Build()+
  
-$tokenResult = $assertion.AcquireTokenForClient(@("https://graph.microsoft.com/.default")). +Un fichier .pfx (format PFX PKCS#12contient : 
-    ExecuteAsync().GetAwaiter().GetResult()+  * la clé publique 
 +  * la clé privée 
 +  * (optionnella chaîne de certificats 
 +  * et est protégé par une passphrase
  
-$Headers = @{ +C’est le seul format portable qui permet : 
-    Authorization = "Bearer $($tokenResult.AccessToken)" +  * d’importer un certificat avec clé privée 
-}+  * d’authentifier une application sur une autre machine 
 +  * d’utiliser le certificat sur Linux / AWX / Docker / CI/CD
  
-Write-Host "Token d'application obtenu via certificat"+^ Format    Contient  ^  Usage  ^ 
 +|.cer|clé publique|Entra ID, SharePoint| 
 +|.pfx|clé publique + clé privée|Authentification| 
 +|.pfx + mdp|portable sécurisé|Serveur, automatisation| 
 +</WRAP>
  
  
-############################################################ +==== Donner accès à l'application sur le site Sharepoint====
-# ÉTAPE 2 – RÉCUPÉRER LE SITE SHAREPOINT PAR SON URL +
-############################################################+
  
-Write-Host "Récupération du site SharePoint..."+   * l'administrateur doit donner l'accès au site voulu
  
-$site = Invoke-RestMethod ` +<code> 
-    -Method GET ` +# deconnexion 
-    -Uri "https://graph.microsoft.com/v1.0/sites/educvaladonlimogesfr.sharepoint.com:$SitePath" ` +Disconnect-MgGraph
-    -Headers $Headers+
  
-$SiteId = $site.id+# Connexion admin 
 +Connect-MgGraph -Scopes "Sites.FullControl.All"
  
-Write-Host "Site trouvé : $($site.displayName)" +# Récupérer le site 
-Write-Host "SiteId $SiteId"+$site = Get-MgSite -SiteId "mondomaine.sharepoint.com:/sites/MonSite"
  
-############################################################ +Donner accès controle total à l'application (fullcontrol / read / write) 
-# ÉTAPE 3 – ATTRIBUER LA PERMISSION WRITE +New-MgSitePermission ` 
-############################################################+  -SiteId $site.Id ` 
 +  -Roles "fullcontrol"
 +  -GrantedToIdentities @{ 
 +      Application = @{ 
 +          Id = "ID application" 
 +          DisplayName = "application" 
 +      } 
 +  } 
 +</code> 
 +  
 +  * Se reconnecter en AppOnly :
  
-Write-Host "Attribution de la permission WRITE à l'application..."+<code> 
 +Connect-MgGraph `   
 +   -TenantId "<TENANT_ID>" `   
 +   -ClientId "<APP_ID>" `   
 +   -CertificateThumbprint "<THUMBPRINT> 
 +</code>
  
-$permissionBody = @{ +  * Puis Tester l’accès au site précis:
-    roles = @("write"+
-    grantedToIdentities = @( +
-        @{ +
-            application = @{ +
-                id = $AppId +
-            } +
-        } +
-    ) +
-} | ConvertTo-Json -Depth 5+
  
-Invoke-RestMethod ` +<code> 
-    -Method POST ` +Get-MgSite -SiteId "mondomaine.sharepoint.com:/sites/MonSite
-    -Uri "https://graph.microsoft.com/v1.0/sites/$SiteId/permissions` +</code>
-    -Headers $Headers ` +
-    -Body $permissionBody ` +
-    -ContentType "application/json"+
  
-Write-Host "Permission Sites.Selected (write) appliquée" 
  
-############################################################ +==== Créer un dossier dans le site SharePoint ====
-# ÉTAPE 4 – TEST : CRÉATION D’UN DOSSIER DANS SHAREPOINT +
-############################################################+
  
-Write-Host "Test : création d’un dossier SharePoint..."+  * Identifier le site SharePoint en itilisant le chemin direct :
  
-$folderBody @{ +<code> 
-    name = $NewFolderName +$site Get-MgSite -SiteId "mondomaine.sharepoint.com:/sites/MonSite" 
-    folder = @{} +$site.Id
-} | ConvertTo-Json+
  
-Invoke-RestMethod ` +=> Afficher le GUID du site du type : 
-    -Method POST ` +xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 
-    -Uri "https://graph.microsoft.com/v1.0/sites/$SiteId/drive/root/children"+</code>
-    -Headers $Headers ` +
-    -Body $folderBody ` +
-    -ContentType "application/json"+
  
-Write-Host "SUCCÈS : le dossier '$NewFolderName' été créé dans SharePoint"+   * Récupérer le drive du site (Documents) : chaque site une document library principale.
  
 +<code>
 +$drive = Get-MgSiteDrive -SiteId $site.Id
 +$drive.Id
 +
 +=> Affiche un DriveId
 +Type : documentLibrary
 </code> </code>
  
-  * Donner accès à l'application sur le site+  * Créer un dossier À LA RACINE (test simple).
  
 <code> <code>
-Grant-MgSitePermission -SiteId $site.Id -Roles "write" -GrantedToIdentities @{ application = @{ id = "APP_ID}+New-MgDriveRootChild ` 
 +  -DriveId $drive.Id 
 +  -AdditionalProperties @{ 
 +      "name= "Test-Dossier" 
 +      "folder" = @{
 +      "@microsoft.graph.conflictBehavior" = "rename" 
 +  }
  
-Grant-MgSitePermission ` 
-    -SiteId $site.Id ` 
-    -Roles "write" ` 
-    -GrantedToIdentities @{ application = @{ id = "APP_ID" } } 
  
 +=> Résultat attendu
 +Id   : 01ABCDEF....
 +Name : Test-Dossier
 +=> Dossier créé avec succès à vérifier dans SharePoint
 </code> </code>
  
-  * Tester l’accès avec Microsoft Graph  (avec client secret)+  * lister les dossier à la racine
  
-Variables requises 
 <code> <code>
-$tenantId     "<TON_TENANT_ID>" +$items Get-MgDriveRootChild -DriveId $drive.Id
-$clientId     = "<APP_ID>" +
-$clientSecret = "<CLIENT_SECRET>" +
-$siteId       = $site.Id+
 </code> </code>
  
-  * Obtenir un token avec l’application+  * Récupérer le dossier General
  
 <code> <code>
-$tokenBody @{ +$parent $items | Where-Object { $_.Name -eq "General" } 
-    client_id     $clientId +$parent.Id 
-    scope         = "https://graph.microsoft.com/.default+</code>
-    client_secret = $clientSecret +
-    grant_type    = "client_credentials" +
-}+
  
-  *  Créer un dossier dans le site SharePoint+  * création du sous-dossier
  
 <code> <code>
-$headers = @{ Authorization = "Bearer $token" } +New-MgDriveItemChild ` 
-$body = '"name""DossierTestCopilot""folder"{} }' +  -DriveId $drive.Id ` 
- +  -DriveItemId $parent.Id ` 
-Invoke-RestMethod ` +  -AdditionalProperties @{ 
-    -Uri "https://graph.microsoft.com/v1.0/sites/$siteId/drive/root/children+      "name" "Test-SousDossier" 
-    -Headers $headers ` +      "folder" = @{} 
-    -Method POST ` +      "@microsoft.graph.conflictBehavior"rename" 
-    -Body $body ` +  }
-    -ContentType "application/json"+
 </code> </code>
- 
 ==== Installer le module module PnP.PowerShell ==== ==== Installer le module module PnP.PowerShell ====
 PnP.PowerShell supporte : PnP.PowerShell supporte :
reseau/cloud/azure/configurerapppoursharepoint.1775419739.txt.gz · Dernière modification : 2026/04/05 22:08 de techer.charles_educ-valadon-limoges.fr