Azure active directory 启用MFA时,如何以非交互方式连接azuread模块?

Azure active directory 启用MFA时,如何以非交互方式连接azuread模块?,azure-active-directory,azure-powershell,arm-template,azure-function-app,multi-factor-authentication,Azure Active Directory,Azure Powershell,Arm Template,Azure Function App,Multi Factor Authentication,我需要使用powershell脚本连接到azure功能应用程序中的广告。(由于它在运行中,我需要在没有提示的情况下执行此操作)我正在尝试以下操作: Import-Module D:\home\site\wwwroot\HttpTrigger1\AzureAD\AzureAD.psd1 -UseWindowsPowershell $creds = Connect-AzureAD -TenantId $tenantId -Credential $Credential 在我的功能应用程序中,我已通过

我需要使用powershell脚本连接到azure功能应用程序中的广告。(由于它在运行中,我需要在没有提示的情况下执行此操作)我正在尝试以下操作:

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时工作,根据我们的用例,无法删除MFA

用例:我有一个ARM模板形式的应用程序,可以作为托管应用程序部署。 ARM模板应该在购买应用程序的用户的租户上部署一组资源。但我需要“客户id” 以及具有O365 mgt api权限的用户/客户租户上的应用程序注册的“客户机密”,作为 my mainTemplate.json.
这个应用程序注册是一次性的,不可能通过ARM模板,这就是为什么我试图通过 powershell。我正在创建powershell功能应用程序,通过使用Azure Active Directory登录启用身份验证。
这种方法背后的想法是,在购买应用程序时,用户在UI(由createUIDefinition.json创建)上填写其他详细信息(如资源组名称和区域),同时单击函数app链接, 登录并在后台运行脚本。脚本应该能够在用户的租户处创建应用程序注册,并提供 返回该应用程序注册的客户端id和客户端密码。

很遗憾,没有

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

解决方法是您可以使用Azure服务主体
。对该函数进行身份验证,并使Azure服务主体执行该任务

什么是Azure服务主体?

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

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

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

通过Powershell创建服务主体

验证服务主体

回到您的场景,要使用服务主体执行Connect AzureAD而不使用交互式登录,您可以使用下面的代码片段

# 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。

不确定我们是否可以在功能应用程序中获得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
谢谢Allen,但这正是我正在使用的。由于启用了MFA,脚本失败。感谢您的回答。但这是一个更大的自动化过程的一部分,场景与解释这个问题的场景类似。因此,还需要在客户机的租户上创建服务主体,这也需要通过脚本或某种自动化来实现。是否有解决方案或解决方法?创建服务主体是一次性工作。一旦创建了服务主体,您就可以以非交互方式重复使用它任意次数。服务主体的事件创建-将连接AzureAD-如果启用MFA,您必须以交互方式通过登录。考虑到安全问题,您无法通过MFA。嘿,我在问题中提供了我的用例的更多详细信息。你能帮我理解在这种情况下我如何使用服务负责人吗?我还在学习这些技术,不太清楚。功能应用程序是在哪里创建的?你不需要同样的应用程序Id吗?