Example #1
0
        public UserResource GetUserResource(ClaimsPrincipal principal)
        {
            var userResource = new UserResource
            {
                DisplayName  = principal.Identity.Name,
                EmailAddress = principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value,
                Username     = principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value,
                ExternalId   = principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value
            };

            if (!string.IsNullOrEmpty(userResource.EmailAddress))
            {
                userResource.Username = userResource.EmailAddress;
            }
            else if (!string.IsNullOrEmpty(userResource.DisplayName))
            {
                userResource.Username = userResource.DisplayName;
            }

            return(userResource);
        }
Example #2
0
        public UserResource MapToUserResource(ClaimsPrincipal principal)
        {
            var userResource = new UserResource
            {
                ExternalId   = GetExternalId(principal),
                Username     = GetUsername(principal),
                EmailAddress = GetEmailAddress(principal),
                DisplayName  = GetDisplayName(principal)
            };

            // Assert we have the bare essentials
            if (string.IsNullOrWhiteSpace(userResource.ExternalId))
            {
                throw new Exception($"The ExternalId resolved by {GetType().Name} was empty but is required. Username: '******' Email: '{userResource.EmailAddress}' Display Name: '{userResource.DisplayName}'");
            }
            if (string.IsNullOrWhiteSpace(userResource.Username))
            {
                throw new Exception($"The Username resolved by {GetType().Name} was empty but is required. ExternalId: '{userResource.ExternalId}' Email: '{userResource.EmailAddress}' Display Name: '{userResource.DisplayName}'");
            }

            return(userResource);
        }