Я зарегистрировал приложение в Azure Active Diretcory и использовал следующий код:
<appSettings>
<add key="TenantId" value="b1f9cb25-7c7a-4ecd-96c1-513c2b42c350"/>
<add key="TenantName" value="myTentantName.onmicrosoft.com"/>
<add key="ClientId" value="d82c0c6a-8c14-4c42-8aca-60c79fcfc9b4"/>
<add key="ClientSecret" value="27?_MOh_qM633Hcccct;cw:@*$9ojcsNxve)rYI"/>
</appSettings>
internal class Settings
{
public const string ResourceUrl = "https://graph.microsoft.com";
public static string TenantId => ConfigurationManager.AppSettings["TenantId"];
public static string TenantName => ConfigurationManager.AppSettings["TenantName"];
public static string ClientId => ConfigurationManager.AppSettings["ClientId"];
public static string ClientSecret => ConfigurationManager.AppSettings["ClientSecret"];
public static string AuthString => "https://login.microsoftonline.com/" + TenantName;
}
class AuthenticationHelper
public static ActiveDirectoryClient GetActiveDirectoryClientAsApplication()
{
Uri servicePointUri = new Uri(Settings.ResourceUrl);
Uri serviceRoot = new Uri(servicePointUri, Settings.TenantId);
ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(
serviceRoot,
async () => await AcquireTokenAsyncForApplication());
return activeDirectoryClient;
}
private static async Task<string> AcquireTokenAsyncForApplication()
{
AuthenticationContext authenticationContext = new AuthenticationContext(Settings.AuthString, false);
ClientCredential clientCred = new ClientCredential(Settings.ClientId, Settings.ClientSecret);
AuthenticationResult authenticationResult =
await authenticationContext.AcquireTokenAsync(
Settings.ResourceUrl,
clientCred);
string token = authenticationResult.AccessToken;
return token;
}
основной класс
var client = AuthenticationHelper.GetActiveDirectoryClientAsApplication();
try
{
var users = await client.Users.OrderBy(user => user.DisplayName).ExecuteAsync();
var foundUser = await client.Users.Where(user => user.ObjectId == "d62d8c6a-dc69-46c1-99c4-36cd672f0c12").ExecuteAsync();
foreach (var user in users.CurrentPage)
{
Console.WriteLine(user.DisplayName + " " + user.ObjectId);
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
Я получаю сообщение об ошибке в главном классе в строке:
var users = await client.Users.OrderBy(user => user.DisplayName).ExecuteAsync();
System.InvalidOperationException: при обработке этого запроса произошла ошибка. ---> System.PlatformNotSupportedException: безопасная двоичная сериализация не поддерживается на этой платформе.