Dieser Artikel enthält ein Beispiel dafür, wie Sie Ihre
Azure-Funktion mit Microsoft Entra ID sichern können, um sichere Aufrufe von
Modern Forms und Automation-Aktionen mithilfe der Aktion
"
Power Automate starten/Azure-Funktion" zu tätigen.
Sichere Azure-Funktion mit dem
Microsoft-Identity Provider
Öffnen Sie die Funktions-App in
portal.azure.com und
suchen Sie
im linken Navigationsmenü nach Einstellungen ->
Authentifizierung
. Klicken Sie auf die
Schaltfläche Identitätsanbieter hinzufügen
und wählen Sie Microsoft-Identitätsanbieter aus.
Wählen Sie unter App-Registrierungstyp eine Option Neue
App-Registrierung erstellen aus (oder wählen Sie vorhandene App aus). Wählen
Sie für den Fall von Multi-Tenant unter Unterstützte Kontotypen die Option Beliebiges Microsoft Entra
Verzeichnis – Multi-Tenant aus.
Wählen Sie die folgenden Optionen aus:
Anforderung
an Clientanwendungen: Anforderungen von bestimmten Clientanwendungen
zulassen
Identitätsanforderung:
Anforderungen von beliebigen Identitäten zulassen
Mandantenanforderung:
Anforderung von bestimmten Mandanten zulassen
Zugriff
einschränken: Authentifizierung erforderlich
Nicht
authentifizierte Anfragen: HTTP 302 Found redirect: empfohlen für Websites
Klicken Sie auf die
Schaltfläche Hinzufügen, um einen Identitätsanbieter hinzuzufügen.
Kopieren Sie die App-ID (Client).
Navigieren Sie für eine mehrinstanzenfähige Konfiguration zu
Ihrer Microsoft-Identitätsanbieter-App. Aktualisieren Sie unter API
verfügbar machen das Feld Anwendungs-ID-URI, um mit https://yourtenant.onmicrosoft.com
Aktivieren von CORS in der Azure-Funktions-App
Die Funktions-App wird von SharePoint aus aufgerufen.
Standardmäßig lassen Azure-Funktions-Apps keine Aufrufe aus anderen Domänen zu,
es sei denn, der ursprungsübergreifende Zugriff ist explizit konfiguriert.
Navigieren Sie in der Azure-Funktions-App im linken Menü zur Option CORS. Fügen
Sie die URL Ihres SharePoint-Mandanten hinzu.
Erteilen der Berechtigung für die Microsoft Entra ID-App in
Ihrem Mandanten
Sie müssen Berechtigungen für diese App für den angegebenen
Mandanten erteilen. Diese App wird portal.azure.com zu Microsoft Entra ID ->
Enterprise-Anwendungen hinzugefügt
. Kopieren Sie die URL Ihrer Azure-Funktions-App, fügen Sie
sie in die Registerkarte des privaten Browsers ein, und melden Sie sich mit dem
Mandantenadministratorkonto an. Aktivieren Sie
die Option Zustimmung im Namen Ihrer
Organisation und Berechtigungen akzeptieren
Erteilen der Microsoft Entra ID-App-Berechtigung für die
Verwendung über SPFx
Um diese App verwenden zu können, müssen Sie das folgende
PowerShell-Skript ausführen, um die Berechtigung "user_impersonation"
Ihrer Microsoft Entra ID-App für die SharePoint
Online-Webclient-Erweiterbarkeits-App
zu erteilen
Im folgenden Skript müssen Sie Folgendes eingeben:
$guidRegex = '^[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$'
do {
$resourceAppId = Read-Host -Prompt "Enter Application (client) ID"
if ($resourceAppId -notmatch $guidRegex) {
Write-Host "Invalid format. Please enter a valid GUID." -ForegroundColor Red
}
} while ($resourceAppId -notmatch $guidRegex)
do {
$scope = Read-Host -Prompt "Enter the scope"
if ([string]::IsNullOrWhiteSpace($scope)) {
Write-Host "Scope cannot be empty. Please try again." -ForegroundColor Yellow
}
} while ([string]::IsNullOrWhiteSpace($scope))
$spOnlineWebClientExtensibilityAppId = "08e18876-6177-487e-b8b5-cf950c1e598c" # SharePoint Online Web Client Extensibility
$resourceGrant = $null
# Prompt to install the required modules if not yet installed
if ($null -eq (Get-Module -ListAvailable -Name Microsoft.Graph.Applications) -or $null -eq (Get-Module -ListAvailable -Name Microsoft.Graph.Identity.SignIns)) {
$response = Read-Host -Prompt "Running this script requires Microsoft.Graph modules that are not yet installed. Install now? (Y/N)"
if ($response -eq "Y") {
if ($null -eq (Get-Module -ListAvailable -Name Microsoft.Graph.Applications)) {
Install-Module -Name Microsoft.Graph.Applications -Scope CurrentUser -Force -AllowClobber
}
if ($null -eq (Get-Module -ListAvailable -Name Microsoft.Graph.Identity.SignIns)) {
Install-Module -Name Microsoft.Graph.Identity.SignIns -Scope CurrentUser -Force -AllowClobber
}
}
else {
Write-Host "The script cannot continue without the Microsoft.Graph modules. Exiting." -ForegroundColor Red
exit
}
}
Connect-MgGraph -Scopes "Application.ReadWrite.All", "Directory.ReadWrite.All" -NoWelcome
try {
# Get the SPFx Service Principal
$spfx = Get-MgServicePrincipal -Filter "appid eq '$spOnlineWebClientExtensibilityAppId'" -ErrorAction Stop
# Get the endpoint service princpal (required to identify the object ID)
$resource = Get-MgServicePrincipal -Filter "appid eq '$resourceAppId'" -ErrorAction Stop
# Get the scopes granted for the endpoint
$spfxGrants = Get-MgServicePrincipalOauth2PermissionGrant -ServicePrincipalId $spfx.Id -ErrorAction Stop
foreach ($spfxGrant in $spfxGrants) {
if ($spfxGrant.ResourceId -eq $resource.Id) {
$resourceGrant = $spfxGrant
break
}
}
# If some scopes have already been granted for the endpoint, we check if the scope we are about to add already exists there
if ($null -ne $resourceGrant) {
if ($resourceGrant.Scope | Select-String $scope -Quiet ) {
Write-Host "$($resource.DisplayName) $scope has already been granted for SharePoint Online Web Client Extensibility" -ForegroundColor Green
continue
}
# The scope does not yet exist; add it to the property and update it
$resourceGrant.Scope += " $scope"
Update-MgOauth2PermissionGrant -OAuth2PermissionGrantId $resourceGrant.Id -Scope $resourceGrant.Scope -ErrorAction Stop | Out-Null
}
# Otherwise, create a new object with the scope
else {
$params = @{
"clientId" = $spfx.id
"consentType" = "AllPrincipals"
"resourceId" = $resource.id
"scope" = $scope
}
New-MgOauth2PermissionGrant -BodyParameter $params -ErrorAction Stop | Out-Null
}
Write-Host "$($resource.DisplayName) $scope granted for SharePoint Online Web Client Extensibility." -ForegroundColor Green
}
catch {
Write-Host "The following error occurred: $_.Exception" -ForegroundColor Red
}
finally{
$_ = Disconnect-MgGraph # Assigning the output to a variable hides it from the terminal
}
Sie können die vertrauenswürdige Berechtigung im SharePoint
Admin Center -> Erweitert -> API-Zugriff anzeigen. Sie können dieses
Skript unten herunterladen.
Auslösen der Azure-Funktion über die Aktion "Power
Automate/Azure-Funktion"
Jetzt können Sie die Aktion "Power Automate /
Azure-Funktion starten" mit der Option "Microsoft Entra ID"
verwenden und sichere Aufrufe an Azure Functions aus modernen Formularen und
Automatisierungsaktionen tätigen.