使用 C# 控制台应用程序查询 Azure AD
Querying Azure AD using c# console application
我正在开发一个简单的 c# 控制台应用程序来查询 Azure AD 并获取给定用户的详细信息。我找到了许多关于查询 azure AD 的有用文章,但其中 none 篇文章满足了我的目的。 GitHub 上发布的示例代码对于我的简单要求来说过于冗长和复杂。
我正在使用下面的代码,但出现令牌错误:
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/{v-sidmis@microsoft.com}?" + 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);
}
但没有得到所需的结果。请帮忙!!!
如果您想使用图形 API 获取用户信息。您需要向您的请求添加令牌 header,如下所示:
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);
}
我们可以在 Azure AD 本机应用程序中找到 ClientId、RedirectURi、tenantId、ResourceUrl:
我正在开发一个简单的 c# 控制台应用程序来查询 Azure AD 并获取给定用户的详细信息。我找到了许多关于查询 azure AD 的有用文章,但其中 none 篇文章满足了我的目的。 GitHub 上发布的示例代码对于我的简单要求来说过于冗长和复杂。 我正在使用下面的代码,但出现令牌错误:
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/{v-sidmis@microsoft.com}?" + 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);
}
但没有得到所需的结果。请帮忙!!!
如果您想使用图形 API 获取用户信息。您需要向您的请求添加令牌 header,如下所示:
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);
}
我们可以在 Azure AD 本机应用程序中找到 ClientId、RedirectURi、tenantId、ResourceUrl: