2016-11-21 63 views
0

我正在開發一個簡單的c#控制檯應用程序來查詢Azure AD並獲取給定用戶的詳細信息。我發現了許多有關查詢天青AD的有用文章,但沒有一篇符合我的目的。發佈在GitHub上的示例代碼太冗長且複雜,因爲我的簡單需求。 我使用下面的代碼,但我得到一個令牌錯誤:使用c#控制檯應用程序查詢Azure AD

static async void MakeRequest() 
     { 
      var client = new HttpClient(); 

      var queryString = HttpUtility.ParseQueryString(string.Empty); 

      /* OAuth2 is required to access this API. For more information visit: 
       https://msdn.microsoft.com/en-us/office/office365/howto/common-app-authentication-tasks */ 



      // Specify values for the following required parameters 
      queryString["api-version"] = "1.6"; 
      // Specify values for path parameters (shown as {...}) 
      // var uri = "https://graph.windows.net/microsoft.onmicrosoft.com/users/{[email protected]}?" + queryString; 

      var uri = "https://graph.windows.net/72f988bf-86f1-41af-91ab-2d7cd011db47/users?api-version=1.6"; 

      var response = await client.GetAsync(uri); 

      if (response.Content != null) 
      { 
       var responseString = await response.Content.ReadAsStringAsync(); 
       Console.WriteLine(responseString); 
      } 


     } 

我進一步搜索的令牌訪問和註冊我的應用程序中的廣告,並用下面的代碼:

var authContext = new AuthenticationContext("AUTHORITY"); 
      string token; 
      try 
      { 
       //var authresult = authContext.AcquireToken("MYAPP_ID", "MYAPP_CLIENTID", "MYAPP_REDIRECTURI"); 
       var authresult = authContext.AcquireToken("https://graph.windows.net", "23b1c65e-5a20-4b88-a474-85c0845782c7", "https://localhost/"); 
       token = authresult.AccessToken; 
      } 
      catch(Exception ex) 
      { 
       Console.WriteLine(ex); 
      } 

但不是獲得所需的結果。請幫忙!!!

回答

2

如果你想使用圖形API來獲取用戶信息。您需要添加標記到您的請求頭像以下:

client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", TokenForUser); 

這裏是代碼片段,可以幫助到列表用戶信息,希望它可以給你一些提示:

string AuthString = "https://login.microsoftonline.com/"; 
string ResourceUrl = "https://graph.windows.net"; 
string ClientId = "***"; 
var redirectUri = new Uri("https://localhost"); 
string TenantId = "e4162ad0-e9e3-4a16-bf40-0d8a906a06d4"; 

AuthenticationContext authenticationContext = new AuthenticationContext(AuthString+TenantId, false); 
AuthenticationResult userAuthnResult = await authenticationContext.AcquireTokenAsync(ResourceUrl, 
    ClientId, redirectUri, new PlatformParameters(PromptBehavior.RefreshSession)); 
TokenForUser = userAuthnResult.AccessToken; 
var client = new HttpClient(); 

var uri = $"https://graph.windows.net/{TenantId}/users?api-version=1.6"; 
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", TokenForUser); 
var response = await client.GetAsync(uri); 
if (response.Content != null) 
{ 
    var responseString = await response.Content.ReadAsStringAsync(); 
    Console.WriteLine(responseString); 
} 

我們可以找到ClientId,RedirectURi,TenantId,Azure AD本機應用程序中的ResourceUrl:

+0

應用程序ID是clientid。 –

+0

此外代碼顯示PlatformParameters上的構建錯誤和用戶的令牌 – dotnetman

+0

我是否需要platformParamerters的任何程序集引用? – dotnetman