c# - 使用 C# 代码查询 Azure Active AD 用户时出现异常
问题描述
我在 Azure Active Directory 中注册了一个应用程序并使用以下代码:
<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;
}
类 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:此平台不支持安全二进制序列化。
解决方案
我通过参考官方示例获得了成功:Azure-Samples / active-directory-dotnet-graphapi-console
唯一的区别是ResourceUrl
不是 https://graph.microsoft.com
。正确的值为https://graph.windows.net
。
那讲得通。因为您尝试使用AD Graph而不是Microsoft Graph
更新
与代码中一样,您正在使用客户端凭据获取令牌。因此,您获得的令牌只有应用程序权限。因此,您需要在 Azure 门户中添加和授予应用程序权限:
推荐阅读
- python - 我正在尝试用 pyglet 制作蛇,但我无法让蛇正确转动
- git - 我该如何解决这个无法远程将参考推送到 section.io 拉取所需的代理图像
- firebird - 文件的“CreateFile(打开)”操作期间的 FirebirdSql 数据客户端 I/O 错误
- python - 下载bz2,读取内存中的压缩文件(避免内存溢出)
- python - Django:如何验证 AbstractBaseUser?
- laravel - Laravel 旋转 SMTP 服务器
- python-3.x - 尝试从 s3 字节对象读取泡菜文件时出现“错误 - 'NoneType' 对象没有属性 'axes'”
- java - JVM会优化未使用的字段吗
- python - Reticulate 在 py_config() 或 repl_python() 上崩溃
- javascript - 查找数组中每个 JavaScript 对象的 null 属性