reseau:cloud:azure:configurerapppoursharepoint
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| reseau:cloud:azure:configurerapppoursharepoint [2026/04/05 22:41] – techer.charles_educ-valadon-limoges.fr | reseau:cloud:azure:configurerapppoursharepoint [2026/04/10 22:24] (Version actuelle) – [Utilisation d'un certificat] techer.charles_educ-valadon-limoges.fr | ||
|---|---|---|---|
| Ligne 7: | Ligne 7: | ||
| * depuis le **portail Azure**, | * depuis le **portail Azure**, | ||
| * en **CLI/ | * en **CLI/ | ||
| - | </ | + | |
| ===== Installer le module Microsoft.Graph de Powershell===== | ===== Installer le module Microsoft.Graph de Powershell===== | ||
| Ligne 14: | Ligne 14: | ||
| ==== Installer Microsoft.Graph de Powershell ==== | ==== Installer Microsoft.Graph de Powershell ==== | ||
| - | | + | |
| * permissions API (Microsoft Graph) : Sites.Selected (Application) | * permissions API (Microsoft Graph) : Sites.Selected (Application) | ||
| - | | + | |
| < | < | ||
| Ligne 100: | Ligne 100: | ||
| -KeyLength 2048 ` | -KeyLength 2048 ` | ||
| -NotAfter (Get-Date).AddYears(4) | -NotAfter (Get-Date).AddYears(4) | ||
| - | |||
| - | $cert.Thumbprint | ||
| - | |||
| - | |||
| - | |||
| </ | </ | ||
| - | * Notez l' | + | * Notez l' |
| < | < | ||
| Ligne 128: | Ligne 123: | ||
| $cert.HasPrivateKey doit renvoyer True | $cert.HasPrivateKey doit renvoyer True | ||
| - | + | === Export du certificat PUBLIC | |
| - | + | ||
| - | * Export du certificat PUBLIC à importer dans Entra ID | + | |
| < | < | ||
| Ligne 138: | Ligne 131: | ||
| </ | </ | ||
| - | | + | |
| + | |||
| + | * Test avec PowerShell (certificat local) | ||
| < | < | ||
| - | Add-MgApplicationKey | + | Connect-MgGraph |
| - | | + | |
| - | | + | |
| + | | ||
| + | |||
| + | => Résultat attendu | ||
| + | Plain TextWelcome To Microsoft Graph! | ||
| </ | </ | ||
| - | ===================== | + | * Puis : |
| - | * script Powershell | + | < |
| + | Get-MgContext | ||
| + | |||
| + | => on doit voir : | ||
| + | |||
| + | ClientId | ||
| + | TenantId | ||
| + | Scopes | ||
| + | AuthType | ||
| + | TokenCredentialType | ||
| + | CertificateThumbprint | ||
| + | </ | ||
| + | |||
| + | === Exporter le certificat | ||
| - | <WRAP center round important> | ||
| - | Il est important de se connecter en tant qu' | ||
| - | </ | ||
| < | < | ||
| - | ############################################################ | + | $thumb = " |
| - | # CONFIGURATION pour l' | + | |
| - | ############################################################ | + | |
| - | $TenantId | + | $cert = Get-Item |
| - | $AppId = " | + | |
| - | $Thumbprint = "EMPREINTE_DU_CERTIFICAT" | + | Export-PfxCertificate ` |
| + | -Cert $cert ` | ||
| + | -FilePath | ||
| + | -Password (ConvertTo-SecureString -String " | ||
| + | </ | ||
| - | $SitePath | + | === Importer un PFX sur une autre machine === |
| - | $NewFolderName | + | |
| + | < | ||
| + | Import-PfxCertificate ` | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | === Générer une clé PKCS#8 === | ||
| - | $SitePath = "/ | + | * Recharger le PFX |
| - | $NewFolderName = " | + | |
| - | ############################################################ | + | < |
| - | # ÉTAPE 1 – OBTENIR UN TOKEN D’APPLICATION (CLIENT CREDENTIALS) | + | $pfxPath = " |
| - | ############################################################ | + | $pfxPassword = " |
| - | Write-Host " | + | $cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2( |
| + | $pfxPath, | ||
| + | $pfxPassword, | ||
| + | [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]:: | ||
| + | ) | ||
| + | </ | ||
| - | $cert = Get-Item " | + | * Récupérer la clé privée RSA |
| - | $assertion | + | < |
| - | WithTenantId($TenantId). | + | $rsa = $cert.PrivateKey |
| - | WithCertificate($cert). | + | </ |
| - | | + | |
| - | $tokenResult = $assertion.AcquireTokenForClient(@(" | + | * Exporter la clé AU FORMAT PKCS#8 |
| - | ExecuteAsync().GetAwaiter().GetResult() | + | |
| - | $Headers | + | < |
| - | Authorization = " | + | # Récupère les paramètres |
| - | } | + | $params |
| - | Write-Host "Token d' | + | # Crée un RSA moderne |
| + | $rsa2 = [System.Security.Cryptography.RSA]:: | ||
| + | $rsa2.ImportParameters($params) | ||
| + | # Export PKCS#8 | ||
| + | $pkcs8 = $rsa2.ExportPkcs8PrivateKey() | ||
| - | ############################################################ | + | $base64 = [System.Convert]:: |
| - | # ÉTAPE 2 – RÉCUPÉRER LE SITE SHAREPOINT PAR SON URL | + | |
| - | ############################################################ | + | |
| + | ) | ||
| - | Write-Host " | ||
| - | $site = Invoke-RestMethod ` | + | $pem = @" |
| - | -Method GET ` | + | -----BEGIN PRIVATE KEY----- |
| - | | + | $base64 |
| - | -Headers $Headers | + | -----END PRIVATE KEY----- |
| + | "@ | ||
| - | $SiteId = $site.id | + | $pem | Set-Content ` |
| + | -Path C: | ||
| + | -Encoding ASCII | ||
| + | </ | ||
| - | Write-Host "Site trouvé : $($site.displayName)" | + | === Résumé |
| - | Write-Host " | + | |
| - | ############################################################ | + | <WRAP center round info> |
| - | # ÉTAPE 3 – ATTRIBUER LA PERMISSION WRITE | + | 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 | ||
| - | Write-Host " | + | La clé privée est déjà stockée dans Windows : |
| + | * dans **Cert: | ||
| + | * 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' | ||
| - | $permissionBody = @{ | + | Un fichier .cer ne permet pas : |
| - | roles = @(" | + | * de s’authentifier |
| - | | + | * de signer |
| - | @{ | + | |
| - | application = @{ | + | |
| - | id = $AppId | + | |
| - | } | + | |
| - | } | + | |
| - | ) | + | |
| - | } | ConvertTo-Json -Depth 5 | + | |
| - | Invoke-RestMethod ` | + | Un fichier |
| - | -Method POST ` | + | * la clé publique |
| - | -Uri " | + | * la clé privée |
| - | | + | * (optionnel) la chaîne de certificats |
| - | -Body $permissionBody ` | + | * et est protégé par une passphrase |
| - | | + | |
| - | Write-Host " | + | C’est le seul format portable qui permet : |
| + | * 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 | ||
| - | ############################################################ | + | ^ Format |
| - | # ÉTAPE 4 – TEST : CRÉATION D’UN DOSSIER DANS SHAREPOINT | + | |.cer|clé publique|Entra ID, SharePoint| |
| - | ############################################################ | + | |.pfx|clé publique + clé privée|Authentification| |
| + | |.pfx + mdp|portable sécurisé|Serveur, | ||
| + | </ | ||
| - | Write-Host "Test : création d’un dossier SharePoint..." | ||
| - | $folderBody | + | ==== Donner accès à l' |
| - | name = $NewFolderName | + | |
| - | folder | + | |
| - | } | ConvertTo-Json | + | |
| - | Invoke-RestMethod ` | + | * l' |
| - | -Method POST ` | + | |
| - | -Uri " | + | |
| - | -Headers $Headers ` | + | |
| - | -Body $folderBody ` | + | |
| - | -ContentType " | + | |
| - | Write-Host "SUCCÈS : le dossier '$NewFolderName' | + | < |
| + | # deconnexion | ||
| + | Disconnect-MgGraph | ||
| + | |||
| + | # Connexion admin | ||
| + | Connect-MgGraph -Scopes | ||
| + | |||
| + | # Récupérer | ||
| + | $site = Get-MgSite -SiteId " | ||
| + | # Donner accès controle total à l' | ||
| + | New-MgSitePermission ` | ||
| + | -SiteId $site.Id ` | ||
| + | -Roles " | ||
| + | -GrantedToIdentities @{ | ||
| + | Application = @{ | ||
| + | Id = "ID application" | ||
| + | DisplayName = " | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| + | |||
| + | * Se reconnecter en AppOnly : | ||
| - | * Donner accès à l' | + | < |
| + | Connect-MgGraph ` | ||
| + | | ||
| + | | ||
| + | | ||
| + | </ | ||
| + | |||
| + | | ||
| < | < | ||
| - | Grant-MgSitePermission | + | Get-MgSite |
| + | </ | ||
| - | Grant-MgSitePermission ` | ||
| - | -SiteId $site.Id ` | ||
| - | -Roles " | ||
| - | -GrantedToIdentities @{ application = @{ id = " | ||
| + | ==== Créer un dossier dans le site SharePoint ==== | ||
| + | |||
| + | * Identifier le site SharePoint en itilisant le chemin direct : | ||
| + | |||
| + | < | ||
| + | $site = Get-MgSite -SiteId " | ||
| + | $site.Id | ||
| + | |||
| + | => Afficher le GUID du site du type : | ||
| + | xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | ||
| </ | </ | ||
| - | | + | * Récupérer le drive du site (Documents) : chaque site a une document library principale. |
| - | Variables requises | ||
| < | < | ||
| - | $tenantId | + | $drive = Get-MgSiteDrive -SiteId $site.Id |
| - | $clientId | + | $drive.Id |
| - | $clientSecret | + | |
| - | $siteId | + | => Affiche un DriveId |
| + | Type : documentLibrary | ||
| </ | </ | ||
| - | * Obtenir | + | * Créer |
| < | < | ||
| - | $tokenBody = @{ | + | New-MgDriveRootChild ` |
| - | | + | -DriveId |
| - | | + | -AdditionalProperties |
| - | client_secret = $clientSecret | + | " |
| - | grant_type | + | " |
| - | } | + | |
| + | } | ||
| - | | + | |
| + | => Résultat attendu | ||
| + | Id | ||
| + | Name : Test-Dossier | ||
| + | => Dossier créé avec succès à vérifier | ||
| + | </ | ||
| + | |||
| + | * lister les dossier à la racine | ||
| < | < | ||
| - | $headers | + | $items = Get-MgDriveRootChild -DriveId |
| - | $body = '{ " | + | </ |
| - | Invoke-RestMethod ` | + | * Récupérer le dossier General |
| - | -Uri " | + | |
| - | | + | < |
| - | | + | $parent = $items | Where-Object { $_.Name |
| - | -Body $body ` | + | $parent.Id |
| - | | + | |
| </ | </ | ||
| + | * création du sous-dossier | ||
| + | |||
| + | < | ||
| + | New-MgDriveItemChild ` | ||
| + | -DriveId $drive.Id ` | ||
| + | -DriveItemId $parent.Id ` | ||
| + | -AdditionalProperties @{ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | </ | ||
| ==== Installer le module module PnP.PowerShell ==== | ==== Installer le module module PnP.PowerShell ==== | ||
| PnP.PowerShell supporte : | PnP.PowerShell supporte : | ||
reseau/cloud/azure/configurerapppoursharepoint.1775421670.txt.gz · Dernière modification : 2026/04/05 22:41 de techer.charles_educ-valadon-limoges.fr
