gpt4 book ai didi

c# - Azure AD - 通过图形服务或声明获取组 AD 名称

转载 作者:行者123 更新时间:2023-12-03 02:45:20 30 4
gpt4 key购买 nike

我正在尝试获取组 Azure AD 的名称,Azure 登录 (openId) 后的内部 token 我收到 json 格式的组 ID,但我需要组名称。

登录后的Json:

Claims

尝试使用GraphService在 Azure 门户中,创建一个 key ,但是当我将这些 key 放入 AuthenticationHeaderValue 中时,我的代码损坏了。

Portal Azure Add Key

var graphServiceClientGr = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("Bearer", token);

return Task.FromResult(0);
}));

我的变量“ token ”与我登录后收到的 token 相同,并且内部有声明。

在内部异常中我收到:

“访问 token 验证失败。无效受众”

Exception

我应该在身份验证中输入哪些正确参数?

在这些调用之后我如何接收 GROUP 的名称?有什么建议吗?我不需要角色应用程序名称,因为我需要组 AD 名称

我想尝试下一行,但我不知道在这些对象内我是否收到组的名称。

在这些行中,我希望收到这些用户的组名称,这些用户对应于这些登录 token Groups

或者使用这些行:

grapServiceClient.Me

最佳答案

根据我的研究,如果我们为 Azure AD 应用程序配置 Groups 声明,它只会返回用户用于登录的组声明值和本地中包含的组的 ObjectID组属性。它无法返回组名称。更多详情请引用documententer image description here

所以如果我们想获取组名称,我们可以使用 Microsoft Graph API为拿到它,为实现它。但是api会返回目录对象和组对象。所以我们需要做一些处理。例如1.注册Azure AD应用

  • 配置 API 权限 enter image description here enter image description here

  • 更新 web.config

  • <appSettings>
    <add key="ida:AppID" value="YOUR APP ID" />
    <add key="ida:AppSecret" value="YOUR APP PASSWORD" />
    <add key="ida:RedirectUri" value="https://localhost:PORT/" />
    <add key="ida:AppScopes" value="User.Read Directory.ReadWrite.All Directory.AccessAsUser.All />
    </appSettings>
  • 将以下代码添加到``Startup.cs
  • private async Task OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification notification)
    {
    var idClient = ConfidentialClientApplicationBuilder.Create(appId)
    .WithRedirectUri(redirectUri)
    .WithClientSecret(appSecret)
    .Build();

    string message;
    string debug;

    try
    {
    string[] scopes = graphScopes.Split(' ');

    var result = await idClient.AcquireTokenByAuthorizationCode(
    scopes, notification.Code).ExecuteAsync();

    message = "Access token retrieved.";
    debug = result.AccessToken;
    }
    catch (MsalException ex)
    {
    message = "AcquireTokenByAuthorizationCodeAsync threw an exception";
    debug = ex.Message;
    }

    notification.HandleResponse();
    notification.Response.Redirect($"/Home/Error?message={message}&debug={debug}");
    }
  • 调用图表 API
  •             var graphClient = new GraphServiceClient(
    new DelegateAuthenticationProvider(
    async (requestMessage) =>
    {
    requestMessage.Headers.Authorization =
    new AuthenticationHeaderValue("Bearer", accessToken);
    }));
    var results = await graphClient.Me.MemberOf.Request().GetAsync();
    var lists = results.ToList();
    Group group;
    foreach (var a in lists) {

    if (a.GetType() == typeof(Group)) {

    group = a as Group;
    var groupId=group.Id;
    var groupName=group.DisplayName;


    }


    }

    有关如何开发应用程序的更多详细信息,请参阅document .

    关于c# - Azure AD - 通过图形服务或声明获取组 AD 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59096299/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com