1

I am working on Azure B2C single Signon. I am using:

Microsoft.Azure.ActiveDirectory.GraphClient;

I want to create a function to assign a user to a group, but it is not working:

var user = this.aadClient.Users.Where(u => u.SignInNames.Any(n => n.Value == "sss@sss.com"))
                                    .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();

both the user and the group are correct. But I am getting this exception on await group.UpdateAsync():

The relationship is already tracked by the context.

I have tried to update the context:

await this.aadClient.Context.SaveChangesAsync();

But I am getting the same exception.

Where is the problem in my Code? Any tips could be helpful. thank you!

Samy Sammour
  • 2,298
  • 2
  • 31
  • 66

1 Answers1

0

this code works for me:

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();

I guess the problem was that the user already has this role.

Samy Sammour
  • 2,298
  • 2
  • 31
  • 66