Esempio n. 1
0
        /// <summary>
        /// Updates user, return int as result
        /// </summary>
        /// <param name="user">User</param>
        /// <param name="password">User password for validation</param>
        /// <returns>Int { 0: operation failed }</returns>
        public async Task<int> UpdateAsync(Todo.Model.Common.IUser user)
        {
            try
            {
                return await repository.UpdateAsync<UserEntity>(Mapper.Map<UserEntity>(user));
            }
            catch (Exception ex)
            {

                throw ex.InnerException;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Updates only username or email
        /// </summary>
        /// <param name="user">UserToUpdate</param>
        /// <param name="password">User password</param>
        /// <returns>IUser</returns>
        public async Task<Todo.Model.Common.IUser> UpdateUserEmailOrUsernameAsync(Todo.Model.Common.IUser user, string password)
        {
            try
            {
                IUnitOfWork uow = repository.CreateUnitOfWork();
                bool passwordValid = false;
                Task<UserEntity> result = null;

                // Check if user is valid
                UserManager<UserEntity> UserManager = CreateUserManager();
                
                    UserEntity userToCheck = await UserManager.FindByIdAsync(user.Id);
                    passwordValid = await UserManager.CheckPasswordAsync(userToCheck, password);
               

                if (passwordValid)
                    result = uow.UpdateWithAddAsync<UserEntity>(Mapper.Map<UserEntity>(user), u => u.Email, u => u.UserName);
                else
                    throw new Exception("Invalid password.");

                await uow.CommitAsync();
                return await Task.FromResult(Mapper.Map<Todo.Model.Common.IUser>(result.Result) as Todo.Model.Common.IUser);
            }
            catch (Exception)
            {

                throw;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Registers adds user
        /// </summary>
        /// <param name="user"></param>
        /// <returns>True if success, false otherwise</returns>
        public async Task<bool> RegisterUser(Todo.Model.Common.IUser user, string password)
        {
            try
            {
                
                UserManager<UserEntity> userManager = this.CreateUserManager();

                IdentityResult result = await userManager.CreateAsync(Mapper.Map<UserEntity>(user), password);

                return result.Succeeded;
            

            }
            catch (Exception ex)
            {

                throw ex.InnerException;
            }
        }