gpt4 book ai didi

c# - 将用户分配给组,Azure AD Graph Api

转载 作者:行者123 更新时间:2023-12-03 04:23:11 25 4
gpt4 key购买 nike

我正在研究 Azure B2C 单点登录。我正在使用:

Microsoft.Azure.ActiveDirectory.GraphClient;

我想创建一个函数来将用户分配到组,但它不起作用:

var user = this.aadClient.Users.Where(u => u.SignInNames.Any(n => n.Value == "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7f0c0c0c3f0c0c0c511c1012" rel="noreferrer noopener nofollow">[email protected]</a>"))
.ExecuteAsync().Result.CurrentPage.FirstOrDefault() as User;

var group = this.aadClient.Groups.Where(x => x.DisplayName.Equals(role, StringComparison.OrdinalIgnoreCase))
.Expand(g => g.Members).ExecuteAsync().Result.CurrentPage.FirstOrDefault() as Group;

group.Members.Add(user);
await group.UpdateAsync();

用户和组都是正确的。但我在 wait group.UpdateAsync() 上遇到此异常:

The relationship is already tracked by the context.

我尝试更新上下文:

await this.aadClient.Context.SaveChangesAsync();

但我也遇到了同样的异常。

我的代码问题出在哪里?任何提示都可能有帮助。谢谢!

最佳答案

这段代码对我有用:

var group = this.aadClient.Groups.Where(x => x.DisplayName.Equals(role, StringComparison.OrdinalIgnoreCase))
.ExecuteAsync().Result.CurrentPage.FirstOrDefault() as Group;

group.Members.Add(user);
await this.aadClient.Context.SaveChangesAsync();

我猜问题是用户已经拥有这个角色。

关于c# - 将用户分配给组,Azure AD Graph Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46601404/

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