2016-05-23 5 views
0

Office 365の資格情報を使用してユーザーがWebサイトにログインしたときにユーザープロファイルデータを取得するためにpowershellコマンドを使用しています。 4人から6人のユーザーが私のウェブサイトにアクセスしてoffice 365のログインボタンを押すと、問題が発生しています。これらの資格情報をpowershellに渡すと、3人のユーザーセッションが作成されますが、私は自分自身をgoogledし、同じことを述べるリンクを見つけた。ここにリンクがある:私は一度に私のウェブサイトへの複数のユーザーを期待する必要があるとして、asp.netの電源シェルコマンドを使用してOffice 365のプロファイルデータを取得しようとする複数のユーザー

https://4sysops.com/forums/topic/office-365-you-have-exceeded-the-maximum-number-of-connections-allowed-3/

私はこのための任意の解像度を持つことができます。ここで

私のサンプルコードは次のとおりです。

try 
     { 
      Collection<PSObject> userList = null; 
      // Create Initial Session State for runspace. 
      InitialSessionState initialSession = InitialSessionState.CreateDefault(); 
      initialSession.ImportPSModule(new[] { "MSOnline" }); 
      // Create credential object. 
      PSCredential credential = new PSCredential(UserCredential.UserName, UserCredential.Password); 
      // Create command to connect office 365. 
      Command connectCommand = new Command("Connect-MsolService"); 
      connectCommand.Parameters.Add((new CommandParameter("Credential", credential))); 
      Command getUserCommand = new Command("Get-MsolUser"); 
      getUserCommand.Parameters.Add((new CommandParameter("UserPrincipalName", UserCredential.UserName))); 

      using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(initialSession)) 
      { 
       // Open runspace. 
       psRunSpace.Open(); 
       //Iterate through each command and executes it. 
       foreach (var com in new Command[] { connectCommand, getUserCommand }) 
       { 
        var pipe = psRunSpace.CreatePipeline(); 
        pipe.Commands.Add(com); 
        // Execute command and generate results and errors (if any). 
        Collection<PSObject> results = pipe.Invoke(); 
        var error = pipe.Error.ReadToEnd(); 
        if (error.Count > 0 && com == connectCommand) 
        { 
         // MessageBox.Show(error[0].ToString(), "Problem in login"); 
         //this.Close(); 
         return null; 
        } 
        if (error.Count > 0 && com == getUserCommand) 
        { 
         // MessageBox.Show(error[0].ToString(), "Problem in getting users"); 
         // this.Close(); 
         return null; 
        } 
        else 
        { 
         userList = results; 
         Session["office365userslist"] = userList; 

        } 
       } 
       // Close the runspace. 
       psRunSpace.Close(); 
      } 
      return userList; 
     } 
     catch (Exception ex) 
     { 
      Response.Write(ex.Message); 
      throw; 
     } 

答えて

1

PowerShellには、シナリオのような種類(Webアプリケーション)にはお勧めできません。

ウェブアプリでユーザーのユーザープロフィールを取得するには、Microsoft Graph API - Get userを使用することをおすすめします。

要求:

GET https://graph.microsoft.com/v1.0/me 

は応答:あなたがGitHub Office 365 Starter Project for ASP.NET MVCにサンプルプロジェクトを参照することができ、あなたのWebアプリでMicrosoftグラフAPIを統合する方法について

HTTP/1.1 200 OK 
Content-type: application/json 
Content-length: 491 

{ 
    "businessPhones": [ 
     "businessPhones-value" 
    ], 
    "displayName": "displayName-value", 
    "givenName": "givenName-value", 
    "jobTitle": "jobTitle-value", 
    "mail": "mail-value", 
    "mobilePhone": "mobilePhone-value", 
    "officeLocation": "officeLocation-value", 
    "preferredLanguage": "preferredLanguage-value", 
    "surname": "surname-value", 
    "userPrincipalName": "userPrincipalName-value", 
    "id": "id-value" 
} 

UPDATE#1

APIを介して交換サーバに免責事項テキストを追加するための任意の代替はありますか?

しかし、グラフAPIは免責事項htmlを設定する機能を提供していません。 Office 365 Developer Platform User Voiceにフィードバックを送信できます。

このシナリオでは、考えられる回避策は要求の順序を決めることができます。

たとえば、すべての要求をキューに入れ、3つのスレッド(最大同時実行性)で要求を処理します。ご参考のため

サンプルコード:

public class SequencedRequestsDemo 
{ 
    private class SampleRequest 
    { 
     public string ActionName { get; set; } 

     public UserCredential UserCredential { get; set; } 
    } 

    private class UserCredential 
    { 
     public string UserName { get; set; } 

     public string Password { get; set; } 
    } 

    private ConcurrentQueue<SampleRequest> _queue = new ConcurrentQueue<SampleRequest>(); 

    public override void Run() 
    { 
     _queue.Enqueue(new SampleRequest { ActionName = "action_name1", UserCredential = new UserCredential() }); 

     _queue.Enqueue(new SampleRequest { ActionName = "action_name2", UserCredential = new UserCredential() }); 

     _queue.Enqueue(new SampleRequest { ActionName = "action_name3", UserCredential = new UserCredential() }); 

     _queue.Enqueue(new SampleRequest { ActionName = "action_name4", UserCredential = new UserCredential() }); 

     _queue.Enqueue(new SampleRequest { ActionName = "action_name5", UserCredential = new UserCredential() }); 

     _queue.Enqueue(new SampleRequest { ActionName = "action_name6", UserCredential = new UserCredential() }); 

     var thread1 = new System.Threading.Thread(() => { 
      WaitForRequest(); 
     }); 

     var thread2 = new System.Threading.Thread(() => { 
      WaitForRequest(); 
     }); 

     var thread3 = new System.Threading.Thread(() => { 
      WaitForRequest(); 
     }); 

     thread1.Start(); 

     thread2.Start(); 

     thread3.Start(); 
    } 

    private void WaitForRequest() 
    { 
     while(true) 
     { 
      SampleRequest request; 

      if (_queue.TryDequeue(out request)) 
      { 
       HandleRequest(request); 
      } 
      else 
      { 
       System.Threading.Thread.Sleep(1000); 
      } 
     } 
    } 

    private void HandleRequest(SampleRequest request) 
    { 
     Console.WriteLine("Handle request {0} - {1}", request.ActionName, System.Threading.Thread.CurrentThread.ManagedThreadId); 
    } 
} 
+0

おかげチェン、我々はパワーシェルを使用して免責HTMLを設定しています。だから私はすべてのコードをパワーシェルで書いた。 APIを使用して交換サーバーに免責文を追加する方法はありますか? –

+0

グラフAPIは免責条項htmlを設定する機能を提供していません。この場合、可能な回避策がリクエストの順序を決めている可能性があります。 –

+0

こんにちはChen。私はちょうど私のサンプルコードを更新します。それを一度チェックしてください。私はシーケンスとしてそれを作ることができますか?あなたはどうすればいいですか? –

関連する問題