Beispiel #1
0
        private LondonTravelUser CreateSystemUser(ExternalLoginInfo info)
        {
            var email = info.Principal.FindFirstValue(ClaimTypes.Email);

            if (string.IsNullOrEmpty(email))
            {
                return(null);
            }

            var givenName = info.Principal.FindFirstValue(ClaimTypes.GivenName);
            var surname   = info.Principal.FindFirstValue(ClaimTypes.Surname);

            var user = new LondonTravelUser()
            {
                CreatedAt      = _clock.GetCurrentInstant().ToDateTimeUtc(),
                Email          = email,
                GivenName      = givenName,
                Surname        = surname,
                UserName       = email,
                EmailConfirmed = false,
            };

            user.Logins.Add(LondonTravelLoginInfo.FromUserLoginInfo(info));

            foreach (var claim in info.Principal.Claims)
            {
                user.RoleClaims.Add(LondonTravelRole.FromClaim(claim));
            }

            return(user);
        }
        private async Task <IdentityResult> UpdateClaimsAsync(LondonTravelUser user, ExternalLoginInfo info)
        {
            bool commitUpdate = false;

            if (user.RoleClaims == null)
            {
                user.RoleClaims = new List <LondonTravelRole>();
                commitUpdate    = true;
            }

            foreach (var claim in info.Principal.Claims)
            {
                bool hasClaim = user?.RoleClaims
                                .Where((p) => p.ClaimType == claim.Type)
                                .Where((p) => p.Issuer == claim.Issuer)
                                .Where((p) => p.Value == claim.Value)
                                .Where((p) => p.ValueType == claim.ValueType)
                                .Any() == true;

                if (!hasClaim)
                {
                    user.RoleClaims.Add(LondonTravelRole.FromClaim(claim));
                    commitUpdate = true;
                }
            }

            if (commitUpdate)
            {
                var result = await _userManager.UpdateAsync(user);

                if (result.Succeeded)
                {
                    _telemetry.TrackClaimsUpdated(user.Id);
                }

                return(result);
            }
            else
            {
                return(IdentityResult.Success);
            }
        }