通过Azure AD B2C授权Azure功能事件网格触发器

问题描述

我的函数应用程序内部有一个Azure事件网格触发器。该功能通过事件订阅订阅了事件网格主题。当我在功能应用程序的身份验证/授权刀片内未配置身份验证时,该功能可以正常工作并被触发。但是,当我从Blade集成B2C AD App时,该主题未传递且该功能未触发。另外,我可以在事件订阅中看到“未经授权”错误。 Function App内的其他HTTP触发器需要B2C流。如何授予对事件网格的独占访问权,以便在没有B2C流的情况下传递此消息?

解决方法

您可以尝试以下方法:

启用事件网格以使用您的Azure AD应用程序:

使用下面的PowerShell脚本在Azure AD应用程序中创建角色和服务主体。您将需要Azure AD应用程序中的租户ID和对象ID:

  1. 修改PowerShell脚本的$ myTenantId以使用您的Azure AD 租户ID。

  2. 修改PowerShell脚本的$ myAzureADApplicationObjectId以使用Azure AD应用程序的对象ID。

  3. 运行修改后的脚本。

    $myTenantId = "<the Tenant Id of your Azure AD Application>"
    
    Connect-AzureAD -TenantId $myTenantId
    
    $myAzureADApplicationObjectId = "<the Object Id of your Azure AD Application>"
    
    $eventGridAppId = "4962773b-9cdb-44cf-a8bf-237846a00ab7"
    
    $eventGridRoleName = "AzureEventGridSecureWebhook"
    
    Function CreateAppRole([string] $Name,[string] $Description)
    {
        $appRole = New-Object Microsoft.Open.AzureAD.Model.AppRole
        $appRole.AllowedMemberTypes = New-Object System.Collections.Generic.List[string]
        $appRole.AllowedMemberTypes.Add("Application");
        $appRole.DisplayName = $Name
        $appRole.Id = New-Guid
        $appRole.IsEnabled = $true
        $appRole.Description = $Description
        $appRole.Value = $Name;
        return $appRole
    }
    
    $myApp = Get-AzureADApplication -ObjectId $myAzureADApplicationObjectId
    $myAppRoles = $myApp.AppRoles
    $eventGridSP = Get-AzureADServicePrincipal -Filter ("appId eq '" + $eventGridAppId + "'")
    
    Write-Host "App Roles before addition of new role.."
    Write-Host $myAppRoles
    
    if ($myAppRoles -match $eventGridRoleName)
    {
        Write-Host "The Azure Event Grid role is already defined.`n"
    }
    else
    {
        $myServicePrincipal = Get-AzureADServicePrincipal -Filter ("appId eq '" + $myApp.AppId + "'")
    
        $newRole = CreateAppRole -Name $eventGridRoleName -Description "Azure Event Grid Role"
        $myAppRoles.Add($newRole)
        Set-AzureADApplication -ObjectId $myApp.ObjectId -AppRoles $myAppRoles
    }
    
    if ($eventGridSP -match "Microsoft.EventGrid")
    {
        Write-Host "The Service principal is already defined.`n"
    }
    else
    {
        $eventGridSP = New-AzureADServicePrincipal -AppId $eventGridAppId
    }
    
    New-AzureADServiceAppRoleAssignment -Id $myApp.AppRoles[0].Id -ResourceId         $myServicePrincipal.ObjectId -ObjectId $eventGridSP.ObjectId -PrincipalId $eventGridSP.ObjectId
    
    Write-Host "My Azure AD Tenant Id: $myTenantId"
    Write-Host "My Azure AD Application Id: $($myApp.AppId)"
    Write-Host "My Azure AD Application ObjectId: $($myApp.ObjectId)"
    Write-Host "My Azure AD Application's Roles: "
    Write-Host $myApp.AppRoles
    

配置事件订阅:

在事件订阅的创建流程中,选择端点类型“ Web Hook”。提供端点URI(事件网格端点的webhuri-https:// FUNCTION_DOMAIN / runtime / webhooks / eventgrid?functionName = {FUNCTION_NAME})后,请单击“创建事件订阅”刀片顶部的其他功能选项卡。

enter image description here

在“附加功能”标签中,选中“使用AAD身份验证”框,然后配置租户ID和应用ID:

  • 从脚本的输出中复制Azure AD租户ID,然后输入 它在AAD租户ID字段中。
  • 从脚本的输出中复制Azure AD应用程序ID,然后 在“ AAD应用程序ID”字段中输入。

enter image description here

编辑:

有关此解决方案的更多详细信息,请访问here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...