コード例 #1
0
        public IdentityUser CreateUserWithSocialProvider(UserLoginInfo login, ClaimsIdentity identity)
        {
            var userProfile = _userProfileSocialService.GetFromSocialProvider(login.LoginProvider, identity);
            var user        = new IdentityUser(GenerateUserName())
            {
                Email = userProfile.Email
            };

            var userCreationResult = _userManager.Create(user);

            if (userCreationResult.Succeeded)
            {
                var userLoginResult = _userManager.AddLogin(user.Id, login);
                if (!userLoginResult.Succeeded)
                {
                    foreach (var error in userLoginResult.Errors)
                    {
                        throw new Exception(error);
                    }
                }
            }
            else
            {
                foreach (var error in userCreationResult.Errors)
                {
                    throw new Exception(error);
                }
            }

            userProfile.UserId = user.Id;
            _userProfileRepository.Add(userProfile);
            _userProfileRepository.SaveChanges();

            return(user);
        }
コード例 #2
0
        public IdentityUser CreateUserWithSocialProvider(UserLoginInfo login, ClaimsIdentity identity)
        {
            var userProfile = _userProfileSocialService.GetFromSocialProvider(login.LoginProvider, identity);

            if (string.IsNullOrEmpty(userProfile.Email))
            {
                throw new Exception("Debes proveer un correo para crear tu cuenta.");
            }
            var user = new IdentityUser(GenerateUserName())
            {
                Email = userProfile.Email
            };

            var userCreationResult = _userManager.Create(user);

            _userManager.AddToRole(user.Id, "Client");
            if (userCreationResult.Succeeded)
            {
                var userLoginResult = _userManager.AddLogin(user.Id, login);
                if (!userLoginResult.Succeeded)
                {
                    foreach (var error in userLoginResult.Errors)
                    {
                        throw new Exception(error);
                    }
                }
            }
            else
            {
                foreach (var error in userCreationResult.Errors)
                {
                    throw new Exception(error);
                }
            }

            userProfile.UserId = user.Id;
            _userProfileRepository.Add(userProfile);
            _userProfileRepository.SaveChanges();

            return(user);
        }