2016-05-04 66 views

回答

4

您可以使用微软图形API - Create User

在Azure AD注册本地客户端应用程序,分配“微软图形”>“读取和写入目录数据”的权限。

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': '[email protected]' 
       }, 
       'userPrincipalName': '[email protected]' 
      }"; 

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

类型“System.FormatException”的未处理的异常发生在Microsoft.IdentityModel.Clients.ActiveDirectory.dll – bijupranavam

+0

其他信息:索引(基于零)必须大于或等于零,并且小于参数列表的大小。 – bijupranavam

+0

请确保您将“app_client_id”替换为实际的应用客户端ID(例如dcd68e75-54d4-xxxx-9dfb-xxxx3833ec1a,没有“{”和“}”)。并确保使用您的实际域名替换“yourdomain”。 –

相关问题