2017-08-17 8 views
1

グラフを使用してwpfアプリケーションの紺碧のADに接続しようとしています。認証が正常に行われ、アクセストークンが返されます。私はこのトークンを使って、自分についての基本情報を得ることもできます。しかし、ディレクトリから何かをリクエストしようとすると、エラーが表示されます。WPFアプリケーションでグラフを使用してAzure AD APIに接続する

Code":"JWT10315 Signature validation failed. Keys tried:

そして次にたくさんのものがあります。すべてが大丈夫と思われる。アプリはAzureに登録されています。正しいアクセス許可が設定されています。私は無知だ。一緒に私を助けることができる人?私のコードは以下の通りです。

//using Microsoft.IdentityModel.Clients.ActiveDirectory; 
using Microsoft.Identity.Client; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace O365_Graph_Connector 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    //graph endpoint 
    //$upn="userPrincipalName eq '" + $user.Id +"'" 
    string url = "https://graph.windows.net/mydomain.com/activities/signinEvents?api-version=beta&`$filter=userPrincipalName eq '[email protected]com'"; 
    //string url = "https://graph.microsoft.com/v1.0/me/"; 
    //Scopes 
    string[] _scopes = new string[] { "Directory.Read.All" }; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     txtOutput.Text = "bla"; 


    } 

    private async void btnConnect_Click(object sender, RoutedEventArgs e) 
    { 

     AuthenticationResult authResult = null; 

     try 
     { 
      if (authResult == null) 
      { 
       authResult = await App.PublicClientApp.AcquireTokenSilentAsync(_scopes, App.PublicClientApp.Users.FirstOrDefault()); 
       Console.WriteLine("authenticated"); 
      } 
     } 
     catch (MsalUiRequiredException ex) 
     { 
      // A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token 
      System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}"); 

      try 
      { 
       Console.WriteLine("trying method2"); 
       authResult = await App.PublicClientApp.AcquireTokenAsync(_scopes); 

      } 
      catch (MsalException msalex) 
      { 
       txtOutput.Text = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}"; 

      } 
     } 
     catch (Exception ex) 
     { 
      txtOutput.Text = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}"; 
      return; 
     } 

     if (authResult != null) 
     { 
      //txtOutput.Text = await GetHttpContentWithToken(url, authResult.AccessToken); 
      String strResult = await GetHttpContentWithToken(url, authResult.AccessToken); 
      txtOutput.Text = strResult; 

     } 
    } 

    /// <summary> 
    /// Perform an HTTP GET request to a URL using an HTTP Authorization header 
    /// </summary> 
    /// <param name="url">The URL</param> 
    /// <param name="token">The token</param> 
    /// <returns>String containing the results of the GET operation</returns> 
    public async Task<string> GetHttpContentWithToken(string url, string token) 
    { 
     var httpClient = new System.Net.Http.HttpClient(); 
     System.Net.Http.HttpResponseMessage response; 
     try 
     { 
      var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url); 
      //Add the token in Authorization header 
      request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); 
      response = await httpClient.SendAsync(request); 
      var content = await response.Content.ReadAsStringAsync(); 
      return content; 
     } 
     catch (Exception ex) 
     { 
      return ex.ToString(); 
     } 
    } 

    private void btnSignOut_Click(object sender, RoutedEventArgs e) 
    { 
     if (App.PublicClientApp.Users.Any()) 
     { 
      try 
      { 
       App.PublicClientApp.Remove(App.PublicClientApp.Users.FirstOrDefault()); 
       this.txtOutput.Text = "User has signed-out"; 
       //this.CallGraphButton.Visibility = Visibility.Visible; 
       //this.SignOutButton.Visibility = Visibility.Collapsed; 
      } 
      catch (MsalException ex) 
      { 
       txtOutput.Text = $"Error signing-out user: {ex.Message}"; 
       } 
      } 
     } 
    } 
} 

答えて

0

string url = " https://graph.windows.net/mydomain.com/activities/signinEvents?api-version=beta& `$filter=userPrincipalName eq '[email protected]'";

あなたの説明によると、あなたはAzure Active Directory sign-in activity report APIにアクセスしています。この問題を確認するには、tutorialに従ってください。私は両方ともアプリのみ、Directory.Read.All範囲で(サインインしているユーザーの権限を委譲)(サインインしているユーザーなしでサービスとして動作するアプリ)と委任スコープをスコープ確認しましたユーザーの情報を取得することもできます。

アクセストークンを使用して郵便受けを介してサインインアクティビティAPIエンドポイントにアクセスすると、私は応答を得て、テナントをAzure ADプレミアムティアにアップグレードするように促してから、ディレクトリを拾い、紺碧のポータルで「会社のブランディング>無料プレミアムトライアルを入手」をクリックしてください。

一般に

enter image description here

、あなたがjwt.ioを活用することができ、次のようにアップグレードしたら、私はサインインアクティビティを取得することができ、次のよう

enter image description here

をさらに、私は同様の問題に遭遇しましたアクセストークンをデコードし、関連するプロパティが正しく含まれていることを確認してください。

+0

私はあなたの最後のスクリーンショットでexcatエラーメッセージを持っています。私がpowershellを使用するとき、それは動作します。したがって、powershellの亜種はwpfの亜種とは違った働きをするようです。 ITには、私がアクセスしようとしているグラフAPIと関連があります。ほとんどの人が働きます。 ITはちょうどこのエラーを生成するものです。私はここで少し損失があります。誰かが、WPFアプリケーションを通じてazureAD APIにアクセスする作業コードの例を持っていますか?私はここで何をすべきか分からない。私たちはazureADのプレミアムを持っています – Molleke

+0

デコードされたアクセストークンからのappidが私のテナントのものと一致しないとき、私は上記のエラーを受け取りました。この問題を絞り込むには、アクセストークンでサインインアクティビティAPIに対するリクエストをシミュレートするために郵便番号を使用することをお勧めします。 –

+0

powershellが機能するので、ネットワークをトレースして、フィドラー経由でアプリケーションと比較することができます。 –

関連する問題