【发布时间】:2017-11-29 02:49:52
【问题描述】:
我知道有 azure 门户来管理组、用户等。
有没有办法以编程方式(在 C# 中使用 web-api 或 sdk)?
提前致谢。
【问题讨论】:
标签: c# azure azure-active-directory
我知道有 azure 门户来管理组、用户等。
有没有办法以编程方式(在 C# 中使用 web-api 或 sdk)?
提前致谢。
【问题讨论】:
标签: c# azure azure-active-directory
使用 C# 和 Fei Xue 提到的 Graph API 库,您可以编写如下代码:
User newUser = new User
{
Id = user.Id,
BusinessPhones = someUser.BusinessPhones,
DisplayName = someUser.DisplayName,
GivenName = someUser.GivenName,
JobTitle = someUser.JobTitle,
Mail = someUser.Mail,
MobilePhone = someUser.MobilePhone,
OfficeLocation = someUser.OfficeLocation,
PreferredLanguage = someUser.PreferredLanguage,
Surname = someUser.Surname,
UserPrincipalName = someUser.UserPrincipalName
};
User createdUser = await graphClient.Users.Request().AddAsync(newUser);
User 是 Graph 包提供的一个类。 graphClient 对象是一个提供连接和认证信息的 GraphServiceClient 对象。
此外,还支持一组核心用户信息。您可以在用户文档中对 Create User 示例的响应中看到它们。文档中还为用户列出了其他属性,但这些属性“属于”SharePoint、Office 365 等应用程序,如果没有该应用程序的许可,则无法使用。
请注意,文档很少,因此有时需要反复试验才能使某些东西发挥作用。
我一直在使用Graph Snippets example 来获得这方面的经验。它有很多使用示例。另一个很棒的资源是 Graph Explorer,它可以让您实时尝试。
如果您遇到问题,我被告知 Azure 和 Graph 开发人员会监控 SO 并快速响应问题。这是我迄今为止的经验。
祝你好运!
【讨论】:
使用 Microsoft Graph REST 创建 Azure AD 用户很容易。这是一个代码示例供您参考:
POST https://graph.microsoft.com/v1.0/users
Authorization: Bearer {token}
Content-type: application/json
{
"accountEnabled": true,
"displayName": "displayName-value",
"mailNickname": "mailNickname-value",
"userPrincipalName": "upn-value@tenant-value.onmicrosoft.com",
"passwordProfile" : {
"forceChangePasswordNextSignIn": true,
"password": "password-value"
}
}
它还使用来自here 的C# 提供了相应的库。有关 Microsoft Graph 的更多详细信息,您可以参考以下链接:
【讨论】:
/api/users?id={id} 如何将请求重定向到 https://graph.microsoft.com/v1.0/users ? Authorization: Bearer {token}中的token是当前登录用户的token吗?还是我的应用程序更通用的令牌?我的 Web 应用程序也托管在 Azure 中。我需要添加 Microsoft Graph 吗?