2016-05-04 6 views

答えて

4

あなたはMicrosoftグラフAPIを使用することができます - Create User

許可「ディレクトリデータを読み書き」>「マイクロソフトグラフ」を割り当てる、アズール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'を置き換えてください。 –