【问题标题】:create user accounts in office 365 using c#.net code dynamically使用 c#.net 代码在 Office 365 中动态创建用户帐户
【发布时间】:2016-05-04 09:01:20
【问题描述】:

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

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

【问题讨论】:

    标签: api office365 account


    【解决方案1】:

    您可以使用 Microsoft Graph API - Create User

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

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

    【讨论】:

    • Microsoft.IdentityModel.Clients.ActiveDirectory.dll 中发生了“System.FormatException”类型的未处理异常
    • 附加信息:索引(从零开始)必须大于或等于零且小于参数列表的大小。
    • 请确保将“app_client_id”替换为实际的应用客户端 ID(例如 dcd68e75-54d4-xxxx-9dfb-xxxx3833ec1a,不带“{”和“}”)。并确保将“yourdomain”替换为您的实际域名。
    • 如果它回答了您关于如何通过 Microsoft Graph API 创建用户的问题,我建议您关闭此问题。答案也将帮助其他有类似问题的人。对于第二个,你可以问另一个关于如何为组创建别名的问题。
    • 我觉得这个答案有丰富的知识,而不是与微软用来演示 Graph API 的所有无用的 GET 命令相比。这是一个很好的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2020-10-20
    相关资源
    最近更新 更多