【问题标题】:How to Connect to azuread module non-interactively when MFA is enabled?启用 MFA 时如何以非交互方式连接到 azuread 模块?
【发布时间】:2020-11-25 01:28:24
【问题描述】:

我需要使用 powershell 脚本在 azure function app 中连接到 AD。 (因为它在功能上我需要在没有提示的情况下这样做)我正在尝试这个:

Import-Module D:\home\site\wwwroot\HttpTrigger1\AzureAD\AzureAD.psd1 -UseWindowsPowershell
$creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential

在我的函数应用中,我通过使用 Azure Active Directory 登录启用了身份验证。 有没有办法在 powershell 脚本中使用该身份验证来连接到 azuread 模块。我的意思是用户单击函数应用程序 url,使用他们的凭据登录,并且可以在脚本中为 connect-azuread 使用身份验证。由于启用了 MFA,当前脚本无法正常工作,根据我们的用例无法将其删除。

用例:我有一个 ARM 模板形式的应用程序,它将被部署为托管应用程序。 ARM 模板应该在用户的租户上部署一组资源,无论谁购买了应用程序。但我需要“客户 ID” 和具有 O365 mgt api 权限的用户/客户租户上的应用程序注册的“客户端密码”,作为输入 我的 mainTemplate.json.
此应用程序注册是一次性的,无法通过 ARM 模板实现,这就是我试图通过以下方式实现上述目标的原因 电源外壳。我正在创建一个 powershell 函数应用,通过使用 Azure Active Directory 登录启用身份验证。
这种方法背后的想法是,在购买应用程序时,在 UI(由 createUIDefinition.json 创建)处填写其他详细信息(如资源组名称和区域)时,用户点击功能应用程序链接, 登录,脚本在后台运行。该脚本应该能够在用户的租户处创建应用注册并提供 返回该应用注册的客户端 ID 和客户端密码。

【问题讨论】:

  • 不确定我们是否可以在函数应用中获取 AAD 凭据。但如果您不想以交互方式登录,可以使用此脚本:$username = "{username}" $password = "{password}" $tenantId = "{teanntId}" $secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd $creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential
  • 谢谢艾伦,但这正是我正在使用的。由于启用了 MFA,脚本会失败。

标签: azure-active-directory azure-powershell arm-template azure-function-app multi-factor-authentication


【解决方案1】:

很遗憾没有!

如果启用了 MFA,您将无法以非交互方式登录。这是有意考虑使其更安全。您也不能通过身份验证。

您可能会使用 Azure 服务主体来解决此问题。获取经过身份验证的函数并让 Azure 服务主体来完成这项工作。

什么是 Azure 服务主体?

服务主体是非交互式 Azure 帐户。与其他用户帐户一样,它们的权限在 Azure Active Directory 中进行管理。

分享一些参考文章以更深入地了解 Azure 服务主体:

创建服务主体(创建是一次性过程)

https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal#app-registration-app-objects-and-service-principals

通过 Powershell 创建服务主体

https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-authenticate-service-principal-powershell#create-service-principal-with-self-signed-certificate

对服务主体进行身份验证

https://docs.microsoft.com/en-us/powershell/azure/authenticate-azureps?view=azps-5.1.0

回到您的场景,要使用服务主体执行 Connect-AzureAD 而无需交互式登录,您可以使用以下 sn-p

# 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 = ""
$thumb = (New-SelfSignedCertificate -CertStoreLocation cert:\localmachine\my -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:\localmachine\my\$thumb" -FilePath c:\temp\examplecert.pfx -Password $pwd

# Load the certificate
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate("C:\temp\examplecert.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

代码简短说明:

上述脚本创建服务主体,在租户级别授予读取访问权限,并在最后使用创建的服务主体连接到 Azure AD。

【讨论】:

  • 感谢您的回答。但这是更大自动化过程的一部分,场景类似于解释这个问题的场景。 stackoverflow.com/questions/64789758/… 因此,还需要在客户端的租户上创建服务主体,这也需要通过脚本或一些自动化。是否有解决方案或解决方法?
  • 服务主体的创建是一次性的工作。创建服务主体后,您可以以非交互方式多次重复使用它。服务主体的事件创建 - 将连接 AzureAD - 如果启用 MFA,您将必须以交互方式传递登录
  • 考虑到安全问题,您不能绕过 MFA。
  • 嘿,我在问题中提供了我的用例的更多详细信息。你能帮我理解在这种情况下如何使用服务主体吗?我还在学习这些技术,不是很清楚。
  • 函数应用在哪里创建?你不需要APP Id吗?
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2010-12-19
  • 2017-09-02
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多