您好,我想延长我的 Azure 应用程序注册的访问 token 生命周期。 我知道已经有很多这样的问题,但它们似乎都不起作用,所以我想用我的问题来回答每个问题,我想我创建了一个新问题。
我使用以下 PowerShell 代码创建 Azure AD 策略以延长生命周期并将其附加到我的应用注册。
# import the azure ad module
Import-Module AzureADPreview
# connect with an admin with proper priviledges
Connect-AzureAD
# specify the policyname and Azure application name
$policyName = "ExtendedLifeTimePolicyADAppRegistration"
$applicationName = "AzureTest"
Write-Host "Create a new policy"
$policy = New-AzureADPolicy -Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"04:00:00","MaxInactiveTime":"04:00:00","MaxAgeMultiFactor":"04:00:00","MaxAgeSingleFactor":"04:00:00"}}') -DisplayName $policyName -IsOrganizationDefault $false -Type "TokenLifetimePolicy"
# Get the Azure AD application
$app = Get-AzureADApplication -Filter "DisplayName eq '$applicationName'"
Write-Host "Apply the new policy"
Add-AzureADApplicationPolicy -Id $app.ObjectId -RefObjectId $policy.Id
Write-Host "Get assigned policies..."
Get-AzureADApplicationPolicy -Id $app.ObjectId
未显示任何错误代码,并且 Get-AzureADApplicationPolicy 返回该策略已连接到我的 Azure 广告应用程序。
以下代码打印访问 token 和过期时间: 我使用此代码从 Azure AD 应用程序注册打印出访问 token 。
disconnect-azurermaccount
if(-not (Get-Module AzureRm.Profile)) {
Import-Module AzureRm.Profile
}
function Get-AzureRmCachedAccessToken()
{
$ErrorActionPreference = 'Stop'
if(-not (Get-Module AzureRm.Profile)) {
Import-Module AzureRm.Profile
}
$azureRmProfileModuleVersion = (Get-Module AzureRm.Profile).Version
# refactoring performed in AzureRm.Profile v3.0 or later
if($azureRmProfileModuleVersion.Major -ge 3) {
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
if(-not $azureRmProfile.Accounts.Count) {
Write-Error "Ensure you have logged in before calling this function."
}
} else {
# AzureRm.Profile < v3.0
$azureRmProfile = [Microsoft.WindowsAzure.Commands.Common.AzureRmProfileProvider]::Instance.Profile
if(-not $azureRmProfile.Context.Account.Count) {
Write-Error "Ensure you have logged in before calling this function."
}
}
$currentAzureContext = Get-AzureRmContext
$currentAzureContext
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile)
Write-Debug ("Getting access token for tenant" + $currentAzureContext.Tenant.TenantId)
$currentAzureContext.TokenCache.ReadItems()
$token = $profileClient.AcquireAccessToken($currentAzureContext.Tenant.TenantId)
echo "####"
$token
echo "####"
}
$tenantId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$ClientID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$passwd = ConvertTo-SecureString $ClientSecret -AsPlainText -Force
$pscredential = New-Object System.Management.Automation.PSCredential($ClientId, $passwd)
#Connect-AzurermAccount -Credential $pscredential -Tenant $tenantId
Add-AzureRmAccount -Credential $pscredential -TenantId $tenantId -ServicePrincipal
Get-AzureRmCachedAccessToken
首先,我认为捕获访问 token 的代码不够有效,因为它已被缓存,因此我在 this link 之后尝试了 MS 文档中的以下代码在 postman 。但这与之前的 PowerShell 代码具有相同的输出。这与 1 小时的默认到期时间相同。
在之前的 SO 问题中,我看到将 IsOrganizationDefault 变量更改为“true”的人确实使 AD 策略发挥作用。 对此有两点评论:
- 我没有权限将此值设置为 true,因为这会影响整个租户。
- 我不想将其设置为 true。因为这也会影响创建应用注册并检索访问 token 的任何其他用户。
有谁看到我错过了什么,我忘记做什么或者我做错了什么。
最佳答案
根据我的测试,目前它仅适用于 -IsOrganizationDefault $true
,无论使用 Add-AzureADServicePrincipalPolicy
或 Add-AzureADApplicationPolicy
,如果 -IsOrganizationDefault $false
,则两者都不起作用。
您应该注意该功能 Configurable token lifetimes in Azure Active Directory (Preview)处于预览阶段,您使用的 powershell 命令也仅在 AzureADPreview
中。我认为该功能可能尚未完全实现,因此导致了该问题。
关于azure - 延长访问 token 生命周期 Azure AD 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61231817/