使用 c#.net 代码在 Office 365 中动态创建用户帐户 [英] create user accounts in office 365 using c#.net code dynamically

查看:28
本文介绍了使用 c#.net 代码在 Office 365 中动态创建用户帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

您可以使用 Microsoft Graph API -

 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 客户端 = 新的 HttpClient();AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resourceUrl,clientId, redirectUri, PromptBehavior.Always);client.DefaultRequestHeaders.Add("Authorization", "Bearer" + authenticationResult.AccessToken);字符串内容 = @"{'accountEnabled':真,'displayName': 'testuser','邮件昵称':'测试','密码配置文件':{'forceChangePasswordNextSignIn':真,'密码':'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);

I want a sample application in c#.net which can create users in Office 365 using Microsoft API .

I wish to do it in code, not using Powershell.

解决方案

You can use the Microsoft Graph API - Create User:

Register a Native Client App on Azure AD, assign the "Microsoft Graph" > "Read and Write Directory Data" permission.

            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);

这篇关于使用 c#.net 代码在 Office 365 中动态创建用户帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆