public string VerifyEmail(string email, string hash)
 {
     if (Request.RequestType.Equals("OPTIONS", StringComparison.InvariantCultureIgnoreCase))
     {
         return null;
     }
     try
     {
         User u = userManager.GetUserByEmail(email);
         if (u != null)
         {
             //User Manager Verify Email (email, hash)
             if (u.verifyEmailHash == hash)
             {
                 bool success = userManager.verifyEmail(u);
                 if (success)
                 {
                     AuthenticaitonEngine authEngine = new AuthenticaitonEngine();
                     string token = authEngine.logIn(u.id, u.userName);
                     JsonModels.RegisterResponse rr = new JsonModels.RegisterResponse();
                     rr.userId = u.id;
                     rr.token = token;
                     return AddSuccessHeader(Serialize(rr));
                 }
                 else
                 {
                     return AddErrorHeader("Error updating user",1);
                 }
             }
             else
             {
                 return AddErrorHeader("Invalid verify email identifier", 1);
             }
         }
         else
         {
             return AddErrorHeader("Invalid Email", 1);
         }
     }
     catch (Exception ex)
     {
         return AddErrorHeader("Something went wrong while verifying this email", 1);
     }
 }
        public string Register(string email, string password, string networkJoinCode = null)
        {
            if (Request != null)
            {
                if (Request.RequestType.Equals("OPTIONS", StringComparison.InvariantCultureIgnoreCase))
                {
                    return null;
                }
            }
            try
            {
                CommunicationManager communicationManager = new CommunicationManager();
                string userName = email.Substring(0, email.IndexOf('@'));
                userName = userName.Replace("+", "");
                RegisterModel model = new RegisterModel { Email = email, UserName = userName, Password = password, ConfirmPassword = password };
                if (ValidationEngine.ValidateEmail(model.Email) != ValidationEngine.Success)
                {
                    return AddErrorHeader("Invalid Email", 1);
                }
                if (!userManager.CheckDuplicateEmail(model.Email))
                {
                    return AddErrorHeader("A user with that email already exists in our database", 1);
                }
                if (ValidationEngine.ValidateUsername(model.UserName) != ValidationEngine.Success)
                {
                    return AddErrorHeader(ValidationEngine.ValidateUsername(model.UserName), 1);
                }
                if (!userManager.CheckDuplicateUsername(model.UserName))
                {
                    return AddErrorHeader("A user with that username already exists in our database", 1);
                }
                if (ValidationEngine.ValidatePassword(model.Password) != ValidationEngine.Success)
                {
                    return AddErrorHeader(ValidationEngine.ValidateUsername(model.Password), 1);
                }
                if (model.Password != model.ConfirmPassword)
                {
                    return AddErrorHeader("Password fields do not match", 1);
                }
                if (ModelState.IsValid)
                {
                    User newUser = model.toUser();
                    newUser.profileURL = newUser.userName;

                    newUser = userManager.CreateUser(newUser, model.Password);

                    userManager.ActivateUser(newUser, true);
                    //communicationManager.SendVerificationMail(userManager.GetProviderUserKey(newUser), newUser.userName, newUser.email);

                    if (networkJoinCode != null)
                    {
                        NetworkManager nm = new NetworkManager();
                        Network network = nm.GetNetworkByIdentifier(networkJoinCode);
                        if (network != null)
                        {
                            string[] emailArray = { email };
                            nm.AddNetworkUsers(network, emailArray);
                        }
                    }
                    userManager.SendVerifyEmail(email);

                    AuthenticaitonEngine authEngine = new AuthenticaitonEngine();
                    string token = authEngine.logIn(newUser.id, newUser.userName);
                    JsonModels.RegisterResponse rr = new JsonModels.RegisterResponse();
                    rr.userId = newUser.id;
                    rr.token = token;
                    return AddSuccessHeader(Serialize(rr));
                }
                else
                {
                    return AddErrorHeader("User Model Not Valid", 1);
                }
            }
            catch (Exception ex)
            {
                logAccessor.CreateLog(DateTime.Now, this.GetType().ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), ex.ToString());
                return AddErrorHeader("Something went wrong while creating this user", 1);
            }
        }
 public string ResetPassword(string password, int userId, string hash)
 {
     if (Request.RequestType.Equals("OPTIONS", StringComparison.InvariantCultureIgnoreCase))  //This is a preflight request
     {
         return null;
     }
     try
     {
         User userForId = userManager.GetUser(userId);
         if (userForId.forgotPasswordHash == hash)
         {
             if (userManager.ChangePassword(userForId, password))
             {
                 AuthenticaitonEngine authEngine = new AuthenticaitonEngine();
                 string token = authEngine.logIn(userForId.id, userForId.userName);
                 JsonModels.RegisterResponse rr = new JsonModels.RegisterResponse();
                 rr.userId = userForId.id;
                 rr.token = token;
                 userForId.forgotPasswordHash = null;
                 userManager.UpdateUser(userForId);
                 return AddSuccessHeader(Serialize(rr));
             }
             else
             {
                 return AddErrorHeader("Error resetting password", 1);
             }
         }
         else
         {
             return AddErrorHeader("Invalid hash", 1);
         }
     }
     catch (Exception ex)
     {
         return AddErrorHeader("Error resetting password", 1);
     }
 }
        public string LogOn(string username, string password)
        {
            if (Request.RequestType.Equals("OPTIONS", StringComparison.InvariantCultureIgnoreCase))  //This is a preflight request
            {
                return null;
            }
            else
            {
                try
                {
                    User user = userManager.GetUser(username);
                    //MembershipUser mu = Membership.GetUser(username);
                    //mu.UnlockUser();
                    //mu.ChangePassword(mu.ResetPassword(), "Mpos104*");
                    if (user == null)
                    {
                        user = userManager.GetUserByEmail(username);
                        if (user != null)
                        {
                            username = user.userName;
                        }
                        else
                        {
                            return AddErrorHeader("The username/email does not exist in the database", 1);
                        }
                    }
                    if (userManager.ValidateUser(user, password))
                    {
                        AuthenticaitonEngine authEngine = new AuthenticaitonEngine();
                        string token = authEngine.logIn(user.id, user.userName);

                        AnalyticsAccessor aa = new AnalyticsAccessor();
                        aa.CreateAnalytic("User Login", DateTime.Now, user.userName);

                        JsonModels.LogOnModel logOnReturnObject = new JsonModels.LogOnModel();
                        logOnReturnObject.userId = user.id;
                        logOnReturnObject.firstName = (user.firstName != null) ? user.firstName : null;
                        logOnReturnObject.lastName = (user.lastName != null) ? user.lastName : null;
                        logOnReturnObject.profileURL = (user.profileURL != null) ? user.profileURL : null;
                        logOnReturnObject.token = token;
                        logOnReturnObject.emailVerified = (user.emailVerified == 1) ? true : false;

                        return AddSuccessHeader(Serialize(logOnReturnObject));
                    }
                    else
                    {
                        return AddErrorHeader("User Information Not Valid", 2);
                    }
                }
                catch (Exception ex)
                {
                    logAccessor.CreateLog(DateTime.Now, this.GetType().ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), ex.ToString());
                    return AddErrorHeader("Something went wrong while trying to log this user in", 1);
                }
            }
        }