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

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

问题描述

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

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

我希望用代码而不是Powershell来实现.

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

推荐答案

您可以使用Microsoft Graph API-

You can use the Microsoft Graph API - Create User:

在Azure AD上注册本机客户端应用程序,分配"Microsoft Graph">读取和写入目录数据"权限.

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天全站免登陆