可以使用AccessToken或Identity令牌作为Connect-AzureAD / MSGraph的凭据吗?



我有一个多租户 AzureAD 应用,该应用在我的 Web 应用的登录用户的上下文中调用许多 PowerShell 脚本。

是否可以将 AccessToken 或 IDToken 传递给 PowerShell 脚本,并让它使用Connect-AzureADcommandlet 创建会话?

我看到凭据重载,也许有一种方法可以使用(或转换(令牌使用此参数?

PS C:Users> get-help connect-azuread -examples
NAME
Connect-AzureAD
SYNOPSIS
Connects with an authenticated account to use Active Directory cmdlet requests.

Example 1: Connect a PowerShell session to a tenant
PS C:> Connect-AzureAD -Confirm
This command connects the current PowerShell session to an Azure Active Directory tenant. The command prompts you
for a username and password for the tenant you want to connect to. The Confirm parameter prompts you for
confirmation.
If multi-factor authentication is enabled for your credentials, you must log in using the interactive option or
use service principal authentication.
Example 2: Connect a session using a variable
PS C:> $Credential = Get-Credential
PS C:> Connect-AzureAD -Credential $Credential
The first command gets the user credentials, and then stores them in the $Credential variable.
The second command connects the current PowerShell session using the credentials in $Credential.
This account authenticates with Azure Active Directory using organizational ID credentials. You cannot use
multi-factor authentication or Microsoft account credentials to run Azure Active Directory cmdlets with this
account.
Example 3: Connect a session as a service principal
# Login to Azure AD PowerShell With Admin Account
Connect-AzureAD
# Create the self signed cert
$currentDate = Get-Date
$endDate = $currentDate.AddYears(1)
$notAfter = $endDate.AddYears(1)
$pwd = "<password>"
$thumb = (New-SelfSignedCertificate -CertStoreLocation cert:localmachinemy -DnsName com.foo.bar -KeyExportPolicy
Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter $notAfter).Thumbprint
$pwd = ConvertTo-SecureString -String $pwd -Force -AsPlainText
Export-PfxCertificate -cert "cert:localmachinemy$thumb" -FilePath c:tempexamplecert.pfx -Password $pwd
# Load the certificate
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate("C:tempexamplecert.pfx", $pwd)
$keyValue = [System.Convert]::ToBase64String($cert.GetRawCertData())

# Create the Azure Active Directory Application
$application = New-AzureADApplication -DisplayName "test123" -IdentifierUris "https://test123"
New-AzureADApplicationKeyCredential -ObjectId $application.ObjectId -CustomKeyIdentifier "Test123" -StartDate
$currentDate -EndDate $endDate -Type AsymmetricX509Cert -Usage Verify -Value $keyValue
# Create the Service Principal and connect it to the Application
$sp=New-AzureADServicePrincipal -AppId $application.AppId
# Give the Service Principal Reader access to the current tenant (Get-AzureADDirectoryRole)
Add-AzureADDirectoryRoleMember -ObjectId 5997d714-c3b5-4d5b-9973-ec2f38fd49d5 -RefObjectId $sp.ObjectId
# Get Tenant Detail
$tenant=Get-AzureADTenantDetail
# Now you can login to Azure PowerShell with your Service Principal and Certificate
Connect-AzureAD -TenantId $tenant.ObjectId -ApplicationId  $sp.AppId -CertificateThumbprint $thumb
This command authenticates the user to Azure Active Directory as a service principal.

下面是一个简单的例子:

Connect-AzureAD
-TenantId {Tenant ID}
-AadAccessToken {AAD Graph Access Token}
-MsAccessToken {Microsoft Graph Access Token}
-AccountId {Your UPN}

请注意,如果必须提供AadAccessToke,因为要连接到 AAD 模块。

MsAccessToken是可选的。但是,如果您需要使用一些需要Microsoft Graph权限的cmd,则应提供MsAccessToken。一个例子是Get-AzureADPolicy.

此处只能使用访问令牌。不支持 ID 令牌。

文档中设置的最后一个参数概述了AADAccessToken参数。

Connect-AzureAD
[-AzureEnvironmentName <EnvironmentName>]
[-TenantId <String>]
-AadAccessToken <String>
[-MsAccessToken <String>]
-AccountId <String>
[-LogLevel <LogLevel>]
[-LogFilePath <String>]
[-InformationAction <ActionPreference>]
[-InformationVariable <String>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]

可以使用 Connect-AzureAD powershell cmdlet 中的access token连接到 Azure AD。但它必须是访问令牌而不是id令牌。它的受众必须是Microsoft图。

相关内容

  • 没有找到相关文章

最新更新