OnExternalLogin issue with member properties not saving
d
Does anyone have much experience with external logins for members? I'm having issues trying to work out how data is supposed to be saved to member properties. I'm using the classic example as per the [documentation](https://docs.umbraco.com/umbraco-cms/reference/security/external-login-providers#example) but struggling to find an example where member properties are updated . Login works, AutoLinking works, my code runs, good values are set as the claim value, and I have properties of the same alias at the claim type, but no values are set against my members. Am I missing a step?
Copy code
OnExternalLogin = (user, loginInfo) => {

                if (loginInfo.Principal.FindFirst(ClaimTypes.GivenName) is Claim firstNameClaim)
                {
                    user.Claims.Add(new IdentityUserClaim<string> {
                        ClaimType = "firstName",
                        ClaimValue = firstNameClaim.Value,
                        UserId = user.Id
                    });
                }

                if (loginInfo.Principal.FindFirst(ClaimTypes.Surname) is Claim surnameClaim)
                {
                    user.Claims.Add(new IdentityUserClaim<string> {
                        ClaimType = "surname",
                        ClaimValue = surnameClaim.Value,
                        UserId = user.Id
                    });
                }

                var continueSignIn = true;
                return continueSignIn;
            }
m
I think you would need to inject
public readonly IMemberService _memberService;
then you can add to the onExternalLogin
Copy code
IMember? member = _memberService!.GetByKey(user.Key);
                    if (member == null)
                    {
                        throw new InvalidOperationException($"Could not find a member with key: {member?.Key}.");
                    }

                    foreach (MemberPropertyModel property in model.MemberProperties.Where(p => p.Value != null).Where(property => member.Properties.Contains(property.Alias)))
                    {
                        member.Properties[property.Alias]?.SetValue(property.Value);
                    }

                    _memberService.Save(member);
or you could just use
member.SetValue("Surname", surname);
rather than looping though a set of properties like I have. If you are using ModelsBuilder you could also fetch the prop alias rather than use a magic string.
d
That would make sense, other than I could have sworn I had it working before where I was just setting the claims in the principal. May need to revert to this approach though.
64 Views