2017-08-17 71 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]'"; 
    //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 Endpoint時,我收到響應並提示我將租戶升級到Azure AD Premium層,然後選取我的目錄並通過激活試用在Azure門戶點擊「公司品牌推廣>免費贈送試用版」。一旦升級,我可以取回登入活動如下:

enter image description here

此外,我遇到了類似的問題如下:

enter image description here

在一般情況下,你可以利用jwt.io解碼您的訪問令牌並確保相關屬性已被正確包含。

+0

我在你上一次截圖中有excat錯誤信息。當我使用powershell它的作品。所以它看起來像Powershell變種與wpf變種不同。 IT也與我嘗試訪問的圖表api有關。大多數人工作。 IT就是這個產生錯誤的人。我在這裏有點不知所措。有人有一個工作代碼的示例,通過WPF應用程序訪問azureAD API?我不知道該怎麼做。我們有azureAD溢價 – Molleke

+0

當解碼的訪問令牌的appid與我的租戶下的appid不匹配時,我收到了上述錯誤。我會建議你使用郵遞員來模擬請求與您的訪問令牌的登錄活動API以縮小這個問題。 –

+0

由於powershell可以工作,您可以跟蹤網絡並通過提琴手與您的應用程序進行比較。 –

相關問題