2016-05-04 76 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」。 –

相關問題