【问题标题】:Connect to Azure AD from Powershell without prompt在没有提示的情况下从 Powershell 连接到 Azure AD
【发布时间】:2019-10-25 04:08:47
【问题描述】:

我们有一个启用了多因素身份验证的 Azure AD 帐户,想知道是否有一种方法可以在没有提示的情况下通过 Powershell 连接到它,即无需 MFA。

我们已经尝试过Connect-AzureAD -Credentials,但是在设置 MFA 时它不会继续:

AADSTS50076:由于进行了配置更改 由您的管理员执行,或者因为您搬到了新位置,您必须使用多因素 身份验证访问...

如果有任何方法可以设置 MFA 并通过 Powershell 进行连接,我们将不胜感激。

【问题讨论】:

标签: powershell azure-active-directory


【解决方案1】:

没有。如果需要 MFA,您不能以用户身份以编程方式登录。

在这种情况下需要交互式身份验证。

如果您更改策略以允许在没有 MFA 的情况下从该计算机进行身份验证,那么它将起作用。

您还可以使用服务主体而不是用户进行身份验证。

【讨论】:

    【解决方案2】:

    有一些复杂的解决方法。

    您可以使用访问令牌直接连接到 Azure AD:

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

    您可以在没有提示的情况下获得带有刷新令牌的访问令牌。

    要简单地获取刷新令牌,一种简单的方法是使用 Fiddler。打开 Fiddler,然后运行 ​​Connect-AzureAD。您将能够找到刷新令牌:

    然后您可以获得一个新的访问令牌并使用它连接到 AAD,如下所示:

    # The refresh token
    $refresh_token="AQABAAAAAACQN9QBRU3jT6bcBQLZNUj7NLUSh_LtiE0dRWb-Vqb9RjUoNjK67G0DlSF65M_w6o1fAvQ******16Z4J0X-MEZSAA"
    
    # Tenant id and account id
    $tenant_id = "hanxia.onmicrosoft.com"
    $account = "jack@hanxia.onmicrosoft.com"
    
    # 1b730954-1685-4b74-9bfd-dac224a7b894 is a public client from Microsoft 
    $clientId = "1b730954-1685-4b74-9bfd-dac224a7b894"
    $uri = "https://login.microsoftonline.com/${tenant_id}/oauth2/token"
    $body = @{grant_type='refresh_token';resource='https://graph.windows.net';client_id=$clientId;refresh_token=$refresh_token}
    $result = Invoke-RestMethod -Method Post -Uri $uri -Body $body
    $accessToken = $result.access_token
    
    # Connect to AAD
    Connect-AzureAD -TenantId $tenant_id -AadAccessToken $accessToken -AccountId $account
    

    结果

    注意

    刷新包含隐私信息。你需要保证它的安全。

    【讨论】:

    • 这很好用,实际上同样的事情可以使用客户端密码来获取访问令牌。但它会带来任何安全问题吗?
    • 对于 Azure AD Rest API,它不会造成任何安全问题。在 Azure AD 中为应用添加权限时。有两种权限。一是委托权限,二是应用权限。使用用户凭证或刷新令牌,您将获得该用户的令牌,通过这种方式,您将能够调用需要委托权限的 API。并且,通过客户端凭证,您将获得应用程序的令牌,这样您就可以调用需要应用程序权限的API。
    【解决方案3】:

    我也遇到过同样的问题。 下面的代码对我来说就像一个魅力。试试下面

    #Install-Module -Name "AzureAD" -Force -Scope CurrentUser
    $contextX = Get-AzContext
    $context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
    $graphToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.microsoft.com").AccessToken
    $aadToken = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, "https://graph.windows.net").AccessToken
    Write-Output "Connected to account $($context.Account.Id)"
    Connect-AzureAD -AadAccessToken $aadToken -AccountId $context.Account.Id -TenantId $context.tenant.id -MsAccessToken $graphToken -Verbose
    

    附:确保已安装 AzureAD 和 MicrosoftGraph 模块,同时 AzContext 已设置为某个订阅。如果您需要任何进一步的帮助,请告诉我。很高兴为您提供帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 2019-11-22
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多