api - 使用 c#.net 代码在 Office 365 中动态创建用户帐户

标签 api office365 account

我想要一个 c#.net 中的示例应用程序,它可以使用 Microsoft API 在 Office 365 中创建用户。

我希望在代码中完成,而不是使用 Powershell。

最佳答案

您可以使用 Microsoft Graph API - Create User :

在 Azure AD 上注册一个本地客户端应用程序,分配“Microsoft Graph”>“读取和写入目录数据”权限。

enter image description here

            string authority = "https://login.windows.net/yourdomain.onmicrosoft.com";

            string clientId = "{app_client_id}";

            Uri redirectUri = new Uri("http://localhost");

            string resourceUrl = "https://graph.microsoft.com";

            HttpClient client = new HttpClient();

            AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);

            AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resourceUrl,
                clientId, redirectUri, PromptBehavior.Always);

            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + authenticationResult.AccessToken);

            string content = @"{
                'accountEnabled': true,
                'displayName': 'testuser',
                'mailNickname': 'test',
                'passwordProfile': {
                    'forceChangePasswordNextSignIn': true,
                    'password': 'pass@wd12345'
                },
                'userPrincipalName': 'testuser@yourdomain.onmicrosoft.com'
            }";

            var httpContent = new StringContent(content, Encoding.GetEncoding("utf-8"), "application/json");

            var response = client.PostAsync("https://graph.microsoft.com/v1.0/users", httpContent).Result;

            Console.WriteLine(response.Content.ReadAsStringAsync().Result);

关于api - 使用 c#.net 代码在 Office 365 中动态创建用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37023065/

相关文章:

php - 检查 WooCommerce 中 Hook 功能破坏网站上的用户角色

ios - 在 iOS 设备中获取默认用户电子邮件

Android 通讯录不显示我的自定义 account_type

c - 在 API 中混合 Lua 代码和 C 代码

iphone - 用于插入非音频相关硬件的 API?

javascript - AngularJS 飞利浦 Hue 项目

office365 - 使用office.js进行office加载项开发时,无法在word文档的书签中插入文本

java - 通过 Play 商店的 Android 应用类别

ms-office - MS Office 应用程序和 MS Office 添加之间有什么区别

sharepoint - 当我在 office365 "sharepoint online"中插入 iframe 时,出现错误