0

I'm upgrading client code that uses the Microsoft Graph nuget package from v4.54 to v5.4. I have code that works in v4 that updates the identities for a user exactly like How to update identities collection for existing B2C User using Microsoft Graph and SDK.

All the updated functions in my code works, e.g. creating users, adding users to groups, etc., except for updating or adding identities to the user. After updating the Post to a Patch as required, the code fails with an unexpected error:

Microsoft.Graph.Models.ODataErrors.MainError 
Code: Request_BadRequest
Message: signInType cannnot be null or empty  [sic]

I've run the request through Fiddler and the signInType is definitely not null.

Code before

user.Identities.Add(new ObjectIdentity
{
    SignInType = "emailAddress",
    Issuer = issuer,
    IssuerAssignedId = issuerAssignedId
});
await client.Users[user.Id].Request().UpdateAsync(user);

Code after

user.Identities.Add(new ObjectIdentity
{
    SignInType = "emailAddress",
    Issuer = issuer,
    IssuerAssignedId = issuerAssignedId
});
await client.Users[user.Id].PatchAsync(user);

I've also tried creating a new user object for the patch (same error):

var identities = user.Identities;
identities.Add(new ObjectIdentity
{
    SignInType = "emailAddress",
    Issuer = issuer,
    IssuerAssignedId = issuerAssignedId
});
var requestBody = new User 
{
    Identities = identities
};
await client.Users[user.Id].PatchAsync(requestBody );

Has anyone successfully updated the Identities collection on a User with Graph v5.x?

dawidg
  • 2,089
  • 2
  • 13
  • 5

1 Answers1

0

Since I saw your questions and I had planed to update to Graph v5.x too, I started working on it and hit the same problem. But fortunately I've found a way to solve it (change the email address in this sample):

            var user = await graphClient.Users[userid].GetAsync(requestConfig =>
            {
                requestConfig.QueryParameters.Select = new string[] { "Identities", "DisplayName", "Id", "AccountEnabled" };
            });


            var newUsr = new Microsoft.Graph.Models.User();
            newUsr.Identities = new List<ObjectIdentity>();
            newUsr.Identities.Add(
                new ObjectIdentity()
                {
                    SignInType = "emailAddress",
                    IssuerAssignedId = newEMail,
                    Issuer = user.Identities.First(i => i.SignInType == "emailAddress").Issuer
                }
            );

            await graphClient.Users[userid].PatchAsync(newUsr);

The trick seems to be, to only include the changed identity (and not both in my case) and also create a new ObjectIdentity instance (it doesn't work with the old instance from the get request) and fill the required attributes.