你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
重要
从 2019 年 2 月开始,我们开始弃用某些早期版本的 Azure Active Directory Graph API,转而支持 Microsoft Graph API。As of February 2019, we started the process to deprecate some earlier versions of Azure Active Directory Graph API in favor of the Microsoft Graph API.
有关详细信息、更新和期限,请参阅 Office 开发人员中心中的 Microsoft Graph 或 Azure AD Graph。For details, updates, and time frames, see Microsoft Graph or the Azure AD Graph in the Office Dev Center.
展望未来,应用程序将使用 Microsoft Graph API。Moving forward, applications should use the Microsoft Graph API.
概述Overview
使用 Active Directory Graph 将用户登录并控制对应用程序和 API 的访问。Sign-on users and control access to applications and APIs with Active Directory Graph.
客户端库Client library
pip install azure-graphrbac
示例Example
备注
创建凭据实例时,需将资源参数更改为 https://graph.windows.netYou need to change the resource parameter to https://graph.windows.net while creating the credentials instance
from azure.graphrbac import GraphRbacManagementClient
from azure.common.credentials import UserPassCredentials
credentials = UserPassCredentials(
'user@domain.com', # Your user
'my_password', # Your password
resource="https://graph.windows.net"
)
tenant_id = "myad.onmicrosoft.com"
graphrbac_client = GraphRbacManagementClient(
credentials,
tenant_id
)
以下代码创建一个用户,直接获取再通过列表筛选获取该用户,然后将其删除。The following code creates a user, get it directly and by list filtering, and then delete it.
from azure.graphrbac.models import UserCreateParameters, PasswordProfile
user = graphrbac_client.users.create(
UserCreateParameters(
user_principal_name="testbuddy@{}".format(MY_AD_DOMAIN),
account_enabled=False,
display_name='Test Buddy',
mail_nickname='testbuddy',
password_profile=PasswordProfile(
password='MyStr0ngP4ssword',
force_change_password_next_login=True
)
)
)
# user is a User instance
self.assertEqual(user.display_name, 'Test Buddy')
user = graphrbac_client.users.get(user.object_id)
self.assertEqual(user.display_name, 'Test Buddy')
for user in graphrbac_client.users.list(filter="displayName eq 'Test Buddy'"):
self.assertEqual(user.display_name, 'Test Buddy')
graphrbac_client.users.delete(user.object_id)