/// <summary>
        /// Gets the password hash asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns>The password hash.</returns>
        /// <exception cref="System.ArgumentNullException">user</exception>
        public Task <string> GetPasswordHashAsync(IUserPoco user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            return(Task.FromResult <string>(user.PasswordHash));
        }
        /// <summary>
        /// Get the user email.
        /// </summary>
        /// <param name="user">User.</param>
        /// <returns>Task.</returns>
        /// <exception cref="System.ArgumentNullException">user.</exception>
        public Task <string> GetEmailAsync(IUserPoco user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            return(Task.FromResult(user.Email));
        }
        /// <summary>
        /// Gets the security stamp asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">user</exception>
        public Task <string> GetSecurityStampAsync(IUserPoco user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            return(Task.FromResult <string>(user.SecurityStamp));
        }
        /// <summary>
        /// Determines whether the user has password asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns>True if has password.</returns>
        /// <exception cref="System.ArgumentNullException">user</exception>
        public Task <bool> HasPasswordAsync(IUserPoco user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            return(Task.FromResult <bool>(!string.IsNullOrWhiteSpace(user.PasswordHash)));
        }
        /// <summary>
        /// Insert a new user.
        /// </summary>
        /// <param name="user">User poco.</param>
        /// <returns>Task.</returns>
        /// <exception cref="System.ArgumentNullException">user</exception>
        public Task CreateAsync(IUserPoco user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            return(userRepository.InsertAsync(user));
        }
        /// <summary>
        /// Delete a user form db.
        /// </summary>
        /// <param name="user">User poco.</param>
        /// <returns>Task.</returns>
        /// <exception cref="System.ArgumentNullException">user</exception>
        public Task DeleteAsync(IUserPoco user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            return(userRepository.DeleteAsync(user.Id));
        }
        /// <summary>
        /// Returns true if the user email is confirmed.
        /// </summary>
        /// <param name="user">User.</param>
        /// <returns>True if the user email is confirmed.</returns>
        /// <exception cref="System.ArgumentNullException">user.</exception>
        public Task <bool> GetEmailConfirmedAsync(IUserPoco user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            return(Task.FromResult(user.EmailConfirmed));
        }
        /// <summary>
        /// Updates a user.
        /// </summary>
        /// <param name="user">User poco.</param>
        /// <returns>Task</returns>
        public Task UpdateAsync(IUserPoco user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            user.DateUpdated = DateTime.UtcNow;
            return(userRepository.UpdateAsync(user));
        }
        /// <summary>
        /// Sets whether the user email is confirmed.
        /// </summary>
        /// <param name="user">User.</param>
        /// <param name="confirmed">Confirmed flag.</param>
        /// <returns>Task.</returns>
        /// <exception cref="System.ArgumentNullException">user.</exception>
        public Task SetEmailConfirmedAsync(IUserPoco user, bool confirmed)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            user.EmailConfirmed = confirmed;
            return(UpdateAsync(user));
        }
        /// <summary>
        /// Gets the user roles asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns>The user roles.</returns>
        /// <exception cref="System.ArgumentNullException">user</exception>
        public async Task <IList <string> > GetRolesAsync(IUserPoco user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var userRoles = await userRoleRepository.GetAsync(p => p.UserId == user.Id, null, "Role");

            return(userRoles.Select(p => p.Role).Select(d => d.Name).ToList());
        }
Пример #11
0
        private async Task SignInAsync(IUserPoco user, bool isPersistent)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

            Session["UserEmail"] = user.Email;
            Session["CompanyId"] = user.CompanyId;

            AuthenticationManager.SignIn(new AuthenticationProperties()
            {
                IsPersistent = isPersistent
            }, identity);
        }
        /// <summary>
        /// Sets the security stamp asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="stamp">The stamp.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">user or stamp.</exception>
        public Task SetSecurityStampAsync(IUserPoco user, string stamp)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (String.IsNullOrEmpty(stamp))
            {
                throw new ArgumentNullException("stamp");
            }

            user.SecurityStamp = stamp;
            return(Task.FromResult(0));
        }
        /// <summary>
        /// Sets the password hash asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="passwordHash">The password hash.</param>
        /// <returns>Task</returns>
        /// <exception cref="System.ArgumentNullException">user or passwordHash.</exception>
        public Task SetPasswordHashAsync(IUserPoco user, string passwordHash)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (String.IsNullOrEmpty(passwordHash))
            {
                throw new ArgumentNullException("passwordHash");
            }

            user.PasswordHash = passwordHash;
            return(Task.FromResult(0));
        }
        /// <summary>
        /// Set the user email.
        /// </summary>
        /// <param name="user">User.</param>
        /// <param name="email">Email</param>
        /// <returns>Task.</returns>
        /// <exception cref="System.ArgumentNullException">user or email</exception>
        public Task SetEmailAsync(IUserPoco user, string email)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (String.IsNullOrEmpty(email))
            {
                throw new ArgumentNullException("email");
            }

            user.Email = email;
            return(UpdateAsync(user));
        }
        /// <summary>
        /// Removes the user from the role asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="roleName">Name of the role.</param>
        /// <exception cref="System.ArgumentNullException"> user or roleName.</exception>
        public async Task RemoveFromRoleAsync(IUserPoco user, string roleName)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (String.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName");
            }

            var role = await roleRepository.GetOneAsync(p => p.Name == roleName);

            var userRole = await userRoleRepository.GetOneAsync(p => p.RoleId == role.RoleId && p.UserId == user.Id);

            await userRoleRepository.DeleteAsync(userRole);
        }
        /// <summary>
        /// Determines whether user is in role asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="roleName">Name of the role.</param>
        /// <returns>True if in role.</returns>
        /// <exception cref="System.ArgumentNullException">user or roleName.</exception>
        public async Task <bool> IsInRoleAsync(IUserPoco user, string roleName)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (String.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName");
            }

            var role = await roleRepository.GetOneAsync(p => p.Name == roleName);

            var count = await userRoleRepository.GetCountAsync(p => p.RoleId == role.RoleId && p.UserId == user.Id);

            return(count > 0);
        }
        /// <summary>
        /// Adds a user to role asynchronous.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="roleName">Name of the role.</param>
        /// <returns>task</returns>
        /// <exception cref="System.ArgumentNullException">user or roleName.</exception>
        public async Task AddToRoleAsync(IUserPoco user, string roleName)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            if (String.IsNullOrEmpty(roleName))
            {
                throw new ArgumentNullException("roleName");
            }

            var role = await roleRepository.GetOneAsync(p => p.Name == roleName);

            var userRole = userRoleRepository.CreateUserRole();

            userRole.RoleId = role.RoleId;
            userRole.UserId = user.Id;

            await userRoleRepository.InsertAsync(userRole);
        }
 /// <summary>
 /// Adds <see cref="IUserPoco"/> model for delete. This will not call Save() method.
 /// </summary>
 /// <param name="model">The model.</param>
 public virtual void AddForDelete(IUserPoco model)
 {
     genericRepository.Delete(model);
 }
 /// <summary>
 /// Adds <see cref="IUserPoco"/> model for update. This will not call Save() method.
 /// </summary>
 /// <param name="model">The model.</param>
 public virtual void AddForUpdate(IUserPoco model)
 {
     genericRepository.Update(mapper.Map<User>(model));
 }
 /// <summary>
 /// Adds <see cref="IUserPoco"/> model for insert. This will not call Save() method.
 /// </summary>
 /// <param name="model">The model.</param>
 public virtual void AddForInset(IUserPoco model)
 {
     genericRepository.Insert(mapper.Map<User>(model));
 }
 /// <summary>
 /// Deletes the specified <see cref="IUserPoco"/> model asynchronous.
 /// </summary>
 /// <param name="model">The model.</param>
 public virtual System.Threading.Tasks.Task DeleteAsync(IUserPoco model)
 {
     genericRepository.Delete(mapper.Map<User>(model));
     return genericRepository.SaveAsync();
 }
 /// <summary>
 /// Deletes the specified <see cref="IUserPoco"/> model.
 /// </summary>
 /// <param name="model">The model.</param>
 public virtual void Delete(IUserPoco model)
 {
     genericRepository.Delete(mapper.Map<User>(model));
     genericRepository.Save();
 }
 /// <summary>
 /// Inserts the specified <see cref="IUserPoco"/> model into the database.
 /// </summary>
 /// <param name="model">The model.</param>
 public virtual void Insert(IUserPoco model)
 {
     genericRepository.Insert(mapper.Map<User>(model));
     genericRepository.Save();
 }