azure - 延长访问 token 生命周期 Azure AD 应用程序

标签 azure azure-active-directory

您好,我想延长我的 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 策略发挥作用。 对此有两点评论:

  1. 我没有权限将此值设置为 true,因为这会影响整个租户。
  2. 我不想将其设置为 true。因为这也会影响创建应用注册并检索访问 token 的任何其他用户。

有谁看到我错过了什么,我忘记做什么或者我做错了什么。

最佳答案

根据我的测试,目前它仅适用于 -IsOrganizationDefault $true,无论使用 Add-AzureADServicePrincipalPolicyAdd-AzureADApplicationPolicy,如果 -IsOrganizationDefault $false,则两者都不起作用。

您应该注意该功能 Configurable token lifetimes in Azure Active Directory (Preview)处于预览阶段,您使用的 powershell 命令也仅在 AzureADPreview 中。我认为该功能可能尚未完全实现,因此导致了该问题。

关于azure - 延长访问 token 生命周期 Azure AD 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61231817/

相关文章:

azure - 随机 WebJob 会失败

.net - 将角色分配给私有(private)应用程序中的客户端 key

azure - 创建 Azure 虚拟桌面 session 主机后,但用户无法访问远程桌面

azure - 我的 Azure AD - SAML 2.0 配置有问题

azure - 无法使用 Microsoft graph API 获取照片

azure - 无法将 Databricks Repo 推送到 GitHub 存储库

azure - 如何在azure上设计微服务系统并使用工作流程按顺序调用它们

reactjs - Azure 应用服务部署 REACT JS 应用程序时出现错误

azure - 获取 Azure 错误 : IDW10203: The 'scope' or 'scp' claim does not contain scopes 'access_as_machine' or was not found

azure - 我收到 Azure REST API 403 禁止错误