Пример #1
0
        public async Task <UserDTO> FacebookLoginAsync(FacebookUserInfo facebookUser)
        {
            var user = await _userManager.FindByEmailAsync(facebookUser.Email);

            if (user == null)
            {
                user = new User
                {
                    SocialNetworking = true,
                    UserName         = facebookUser.Email ?? facebookUser.UserId,
                    FirstName        = facebookUser.Name.Split(' ')[0],
                    Email            = facebookUser.Email ?? "*****@*****.**",
                    LastName         = facebookUser.Name.Split(' ')[1],
                    ImagePath        = "default_user_image.png",
                    EmailConfirmed   = true,
                    RegistredOn      = DateTime.Now,
                    UserProfile      = new UserProfile
                    {
                        Birthday = DateTime.Parse(facebookUser.Birthday, CultureInfo.InvariantCulture),
                        GenderID = _repoWrapper
                                   .Gender
                                   .FindByCondition(x => x.Name == facebookUser.Gender)
                                   .FirstOrDefault()?.ID,
                    }
                };
                var createResult = await _userManager.CreateAsync(user);

                if (createResult.Succeeded && user.Email != "*****@*****.**")
                {
                    var emailContent = _emailContentService.GetAuthFacebookRegisterEmail();
                    await _emailSendingService.SendEmailAsync(user.Email, emailContent.Subject, emailContent.Message, emailContent.Title);
                }
                await _userManager.AddToRoleAsync(user, Roles.RegisteredUser);
            }
            await _signInManager.SignInAsync(user, false, null);

            return(_mapper.Map <User, UserDTO>(user));
        }