2016-11-21 123 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