/// <summary>
 /// Создать сущность пользователя.
 /// </summary>
 /// <param name="input">Ввод.</param>
 /// <returns>Задача с созданным пользователем.</returns>
 public async Task <DataEntityObjectUser> CreateUserEntity(HostBasePartAuthJobUserEntityCreateInput input)
 {
     return(await AddUser(
                input.UserManager,
                input.RoleManager,
                input.UserName,
                input.Password,
                input.Email,
                input.FullName,
                input.RoleNames,
                input.Claims,
                input.LoginProvider,
                input.ProviderDisplayName,
                input.ProviderKey,
                errors => new HostBasePartAuthJobUserEntityCreateException(errors)
                ).CoreBaseExtTaskWithCurrentCulture(false));
 }
        private async Task <CoreBaseExecutionResultWithData <DataEntityObjectUser> > CreateUserEntity(
            ILogger logger,
            HostBasePartAuthJobUserEntityCreateService job,
            HostBasePartAuthJobUserEntityCreateInput input
            )
        {
            var result = new CoreBaseExecutionResultWithData <DataEntityObjectUser>();

            try
            {
                result.Data = await job.Execute(input).CoreBaseExtTaskWithCurrentCulture(false);

                job.OnSuccess(logger, result, input);
            }
            catch (Exception ex)
            {
                job.OnError(ex, logger, result);
            }

            return(result);
        }
        private async Task <DataEntityObjectUser> AutoProvisionUserAsync(
            string provider,
            string providerUserId,
            IEnumerable <Claim> claims,
            ILogger logger,
            HostBasePartAuthJobUserEntityCreateService jobUserEntityCreate,
            RoleManager <DataEntityObjectRole> roleManager,
            UserManager <DataEntityObjectUser> userManager
            )
        {
            // create a list of claims that we want to transfer into our store
            var filtered = new List <Claim>();

            // user's display name
            var name = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Name)?.Value ??
                       claims.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value;

            if (name != null)
            {
                filtered.Add(new Claim(JwtClaimTypes.Name, name));
            }
            else
            {
                var first = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.GivenName)?.Value ??
                            claims.FirstOrDefault(x => x.Type == ClaimTypes.GivenName)?.Value;
                var last = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.FamilyName)?.Value ??
                           claims.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value;
                if (first != null && last != null)
                {
                    filtered.Add(new Claim(JwtClaimTypes.Name, first + " " + last));
                }
                else if (first != null)
                {
                    filtered.Add(new Claim(JwtClaimTypes.Name, first));
                }
                else if (last != null)
                {
                    filtered.Add(new Claim(JwtClaimTypes.Name, last));
                }
            }

            // email
            var email = claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Email)?.Value ??
                        claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value;

            if (email != null)
            {
                filtered.Add(new Claim(JwtClaimTypes.Email, email));
            }

            var input = new HostBasePartAuthJobUserEntityCreateInput
            {
                Email               = email,
                LoginProvider       = provider,
                ProviderDisplayName = provider,
                ProviderKey         = providerUserId,
                RoleManager         = roleManager,
                RoleNames           = claims.HostBasePartAuthExtGetRoleNamesFromClaims(),
                UserManager         = userManager,
                UserName            = name
            };

            var execResult = await CreateUserEntity(logger, jobUserEntityCreate, input).CoreBaseExtTaskWithCurrentCulture(false);

            if (!execResult.IsOk)
            {
                throw new Exception(string.Join(". ", execResult.ErrorMessages));
            }

            return(execResult.Data);
        }