azure - 通过 ID 获取用户名 Azure AD

标签 azure asp.net-core azure-active-directory azure-ad-msal microsoft-graph-sdks

我们使用 Azure AD 进行身份验证,并将用户 ID(对象 ID)存储在我们的 API 中。我们需要将这些ID解析为用户名。我尝试使用 Microsoft graph 和 On-Behalf-Of 流程将我们的 token 交换到具有所需范围的另一个 token ,但它非常复杂,并且需要用户的 User.Read.All 权限。

所以我的问题是 - 是否还有其他方法可以将用户 ID 解析为名称?

最佳答案

通过 ID 获取任何用户的名称 ---> 然后您必须给应用程序 api permission对于User.Read.All、User.ReadWrite.All、Directory.Read.All、Directory.ReadWrite.AllRead.All 允许您获取任何用户的名称。

enter image description here

然后您必须使用客户端凭证流来获取授权并调用图 api。恐怕代办不适合你。试试下面我的代码。

using Microsoft.Graph;
using Azure.Identity;

var scopes = new[] { "https://graph.microsoft.com/.default" };
string tenantId = "TenantId";
string clientId = "ClientId";
string clientSecret = "ClientSecret";
var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var user = await graphClient.Users["user_id"].Request().GetAsync();

关于azure - 通过 ID 获取用户名 Azure AD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74124841/

相关文章:

asp.net-mvc - IApplicationBuilder.New() 的目的是什么

azure - 如何从 Azure Active Directory 获取 postman 中的用户声明?

azure - 在生产中存储 DocumentDB Endpoint 和 Key 的位置

sql-server - 还原数据库失败 - 弹性池中的 Azure SQL 数据库

c# - Windows Server 2008 R2 共同托管遗留 .Net 应用程序与 .Net Core 应用程序

c# .Net Core 3.1 Azure App Service Windows - 应用程序无法检索应用程序设置

Azure 静态 Web 应用 - 使用 Azure AD 自定义身份验证

azure - 如何使用 .Net API 中的托管标识连接 Azure 日志分析工作区

Azure 应用程序网关无法上传 pfx 证书

Azure OLAP 多维数据集给出错误的总计