コード例 #1
0
        public async Task <bool> RegisterUser(UserInformationViewModel userInformation, string role)
        {
            if (string.IsNullOrEmpty(role))
            {
                role = "Member";
            }
            var user = await _userManager.FindByNameAsync(userInformation.Username);

            if (user == null)
            {
                user = new User
                {
                    Id       = Guid.NewGuid().ToString(),
                    UserName = userInformation.Username,
                    Email    = userInformation.Username
                };

                var result = await _userManager.CreateAsync(user, userInformation.Password);

                if (result.Succeeded)
                {
                    var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var userToConfirm = await _userManager.FindByEmailAsync(user.Email);

                    if (userToConfirm != null)
                    {
                        var resultFromConfirmation = await _userManager.ConfirmEmailAsync(user, token);

                        if (resultFromConfirmation.Succeeded)
                        {
                            await _userManager.AddToRoleAsync(user, role);

                            //add user information
                            var userInformationToCurrentUser = new UserInformation
                            {
                                IdUser   = userToConfirm.Id,
                                Clasa    = userInformation.Clasa,
                                Nume     = userInformation.Nume,
                                Prenume  = userInformation.Prenume,
                                Profesor = userInformation.Profesor,
                                Scoala   = userInformation.Scoala,
                                Username = userInformation.Username
                            };
                            await _userInformationRepository.AddAsync(userInformationToCurrentUser);

                            return(true);
                        }
                    }
                    else
                    {
                        //error
                        return(false);
                    }
                }
                else
                {
                    //error
                    return(false);
                }
            }
            return(false);
        }
コード例 #2
0
 public async Task AddUserInfo(UserInformationModel userInformation)
 {
     var entity = _mapper.Map <UserInformation>(userInformation);
     await _repository.AddAsync(entity);
 }