Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new user from the registration form. Hashes the password and inserts into the database.
        /// </summary>
        /// <param name="rfo">Form object from the registration form</param>
        /// <returns>Returns created user</returns>
        public async Task <User> CreateUser(RegisterFormObject rfo)
        {
            rfo.Password = IdentityBasedHasher.HashPassword(rfo.Password).ToHashString();
            var user = rfo.ToUser();

            using (var db = _conn.Open())
            {
                var userId = await db.InsertAsync(user);

                user.Id = (int)userId;
            }
            return(user);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Checks to see if the username is valid and available in the database
 /// </summary>
 /// <param name="rfo">Form object passed in from the registration form</param>
 /// <param name="modelState">Current ModelState of the controller</param>
 /// <returns>false if there are any errors, true if user is valid and available</returns>
 public async Task <bool> CheckUser(RegisterFormObject rfo, ModelStateDictionary modelState)
 {
     if (!rfo.UserName.All(c => char.IsLetterOrDigit(c)))
     {
         modelState.AddModelError("UserName", "UserName can only contain letters and numbers");
         return(false);
     }
     using (var db = _conn.Open())
     {
         if (await db.ExistsAsync <User>(u => u.Email == rfo.Email))
         {
             modelState.AddModelError("Email", "Email already registered");
             return(false);
         }
         if (await db.ExistsAsync <User>(u => u.UserName == rfo.UserName))
         {
             modelState.AddModelError("UserName", "UserName already taken");
             return(false);
         }
     }
     return(true);
 }