Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Powershell向平台SPA注册Azure应用程序_Azure_Powershell_Azure Active Directory - Fatal编程技术网

如何通过Powershell向平台SPA注册Azure应用程序

如何通过Powershell向平台SPA注册Azure应用程序,azure,powershell,azure-active-directory,Azure,Powershell,Azure Active Directory,我们使用PowerShell设置Azure部署,该部署与其他Azure资源一起创建应用程序注册 简化代码如下: $appRegistration = New-AzADApplication ` -DisplayName $applicationName ` -HomePage "$webAppUrl" ` -IdentifierUris "api://$webAppName"; 为此,我们添加了重定向URI,如下所示: if ($

我们使用PowerShell设置Azure部署,该部署与其他Azure资源一起创建应用程序注册

简化代码如下:

$appRegistration = New-AzADApplication `
    -DisplayName $applicationName `
    -HomePage "$webAppUrl" `
    -IdentifierUris "api://$webAppName";
为此,我们添加了重定向URI,如下所示:

if ($redirectUris -notcontains "$webAppUrl") {
    $redirectUris.Add("$webAppUrl");    
    Write-Host "Adding $webAppUrl to redirect URIs";
}

if ($redirectUris -notcontains "$webAppUrl/aad-auth") {
    $redirectUris.Add("$webAppUrl/aad-auth");
    Write-Host "Adding $webAppUrl/aad-auth to redirect URIs";
}

Update-AzADApplication `
    -ApplicationId $applicationId `
    -IdentifierUris "api://$applicationId" `
    -ReplyUrl $redirectUris | Out-Null
这非常有效,并创建了“web”平台上的应用程序注册。看起来是这样的:

if ($redirectUris -notcontains "$webAppUrl") {
    $redirectUris.Add("$webAppUrl");    
    Write-Host "Adding $webAppUrl to redirect URIs";
}

if ($redirectUris -notcontains "$webAppUrl/aad-auth") {
    $redirectUris.Add("$webAppUrl/aad-auth");
    Write-Host "Adding $webAppUrl/aad-auth to redirect URIs";
}

Update-AzADApplication `
    -ApplicationId $applicationId `
    -IdentifierUris "api://$applicationId" `
    -ReplyUrl $redirectUris | Out-Null

我的问题是如何使用PowerShell将这些重定向URI置于“SPA”平台下?如下图所示,这是在门户上手动完成的

有人试图以编程方式为SPA添加重定向URI,但无法执行,因为它默认位于Web部分下

他能够通过使用Azure CLI发布到Graph API来解决此问题:

az rest `
    --method PATCH `
    --uri 'https://graph.microsoft.com/v1.0/applications/{id}' `
    --headers 'Content-Type=application/json' `
    --body "{spa:{redirectUris:['http://localhost:3000']}}"

看起来,内置命令中没有这样做的功能,您可以直接在powershell中调用MS Graph

您可以参考下面的示例为我工作,确保通过
Connect AzAccount
登录的服务负责人/用户帐户具有调用API的权限

$objectId = "xxxxxxxxxxxxxxxx"
$redirectUris = @()
$webAppUrl = "https://joyweb.azurewebsites.net"
if ($redirectUris -notcontains "$webAppUrl") {
    $redirectUris += "$webAppUrl"   
    Write-Host "Adding $webAppUrl to redirect URIs";
}

if ($redirectUris -notcontains "$webAppUrl/aad-auth") {
    $redirectUris += "$webAppUrl/aad-auth"
    Write-Host "Adding $webAppUrl/aad-auth to redirect URIs";
}

$accesstoken = (Get-AzAccessToken -Resource "https://graph.microsoft.com/").Token
$header = @{
    'Content-Type' = 'application/json'
    'Authorization' = 'Bearer ' + $accesstoken
}
$body = @{
    'spa' = @{
        'redirectUris' = $redirectUris
    }
} | ConvertTo-Json

Invoke-RestMethod -Method Patch -Uri "https://graph.microsoft.com/v1.0/applications/$objectId" -Headers $header -Body $body
在门户中检查结果:


谢谢你提供的信息。在搜索解决方案时,我确实看到了这个线程,但我希望能够使用PowerShell而不是Azure CLI,因为我们已经有了一个广泛的PowerShell脚本,用于部署。我需要的是一种使用invoke RestMethod或invoke WebRequest调用同一个REST调用的方法(这会变得很棘手,因为我需要提取访问令牌)Brilliant,我已经找到了几乎相同的解决方案,只是我正在做一些更复杂的事情来获取访问令牌。谢谢我必须做的一个更改是使用应用程序注册的对象Id而不是应用程序Id。另外,可能是由于传播延迟,我在运行
Invoke RestMethod
时得到了一个“resource not found”。为了克服这个问题,我切换到
调用WebRequest
,并在它们之间添加了重试和等待$response=Invoke WebRequest`-Method Patch`-Uri”“`-Headers$header`-Body$Body-MaximumRetryCount 3-RetryIntervalSec 10@浮动飞盘我用
objectId
测试它,从你的问题复制时忘记更改变量名,请检查更新的答案。