Azure AD-不同租户中主帐户和来宾帐户之间的链接

时间:2019-10-21 13:32:38

标签: azure-active-directory

在一个受到严格监管的上下文中,我们正在对所有Azure AD进行审核,我们正在努力应对以下用例。任何帮助表示赞赏。

上下文:

    公司中的
  • 1个主要Azure AD租户,这是一个O365租户 (标记为“用户租户”)
  • 其他多个Azure AD租户 公司,即“应用程序”租户:
    • 每个 部门/业务部门有自己的应用程序租户(用于 隔离目的),以管理其Azure资源(一个或多个) 订阅)
    • 一些来自用户租户(O365)的用​​户被邀请参加 一些应用程序租户(Azure)作为来宾帐户

用例解决:

  • 员工离开公司并从“用户”租户(O365)中撤出
  • 公司需要从已将其添加为来宾帐户的所有“应用程序”租户中删除该员工
  • 是否有一种方法可以基于O365租户自动/以编程方式识别并删除该员工在不同AAD租户中的所有帐户?
  • 我们可以利用Ali帐户(主要访客)之间的公共“链接”来识别/删除AAD用户吗?
  • 解决方法?最佳做法?

1 个答案:

答案 0 :(得分:0)

根据您的描述,您可以在主要租户中注册多租户应用enter image description here 创建应用后,为其创建一个秘密并记下它的应用ID 秘密,稍后我们将使用它来连接到您的租户: enter image description here

为此应用程序授予Microsoft Graph API的“ User.ReadWrite.All”权限,因为我们将调用Microsoft Graph API删除用户: enter image description here 不要忘记单击“为您的租户授予管理员同意”按钮以完成分配过程。

通过链接让其他租户的管理员同意使用此应用程序:

https://login.microsoftonline.com/<your tenant ID>/adminConsent?client_id=<your multi-tenant app ID>

enter image description here 并且此应用程序将作为服务原则添加到其他租户中: enter image description here 这样我们就可以使用此应用对所有租户进行一些操作。

只需运行下面的Powershell即可从您的所有租户中删除一个用户,我已经在我这边进行了测试,它可以为我工作。顺便说一句,请在运行它之前删除所有目录角色,否则,如果为用户分配了一些管理员角色,则会出现403错误。

#config your multi tenant app id and secret here.
$appid = "<YOUR MULTI TENANT APP ID>"
$secret = "<YOUR MULTI TENANT APP SECRET>"
$userEmailaddress = "<EMAIL ADDRESS OF THE USER YOU WANT TO DELETE>"

#config all your tenents here
$tenants = "<TENANT 1 ID/NAME>","TENANT 2 ID/NAME",...

#check and delete user by email address in all tenants 
foreach($tenant in $tenants){

    #get token to call microsoft graph api to delete users 
    $body=@{
        "grant_type"="client_credentials";
        "resource"="https://graph.microsoft.com";
        "client_id"=$appid;
        "client_secret" = $secret
    }
 
    $result=Invoke-RestMethod -Uri "https://login.windows.net/$tenant/oauth2/token" -Method POST -Body $body 


    #get user object from tenant 
    $userguestPrefix = $userEmailaddress.Replace('@','_')
    $searchURL = "https://graph.microsoft.com/v1.0/users?`$filter=startswith(userPrincipalName,'$userEmailaddress') or startswith(userPrincipalName,'$userguestPrefix')"

    $searchUserResult = Invoke-RestMethod -Uri $searchURL -Method GET -Headers @{'Authorization'='Bearer '+$result.access_token}

    #remove the user from tenant
    if($searchUserResult.value.Length -eq 1){
        echo "remove:"$searchUserResult.value[0].displayName" from tenant : $tenant"
        $removeURL = "https://graph.microsoft.com/v1.0/users/" + $searchUserResult.value[0].id
        Invoke-RestMethod -Uri $removeURL -Method DELETE -Headers @{'Authorization'='Bearer '+$result.access_token}
    }

}

如果您还有其他疑问,请随时告诉我。