public static Return<DataContracts.ActivateUserData> Submit(DataContracts.ActivateUserData activateUserData)
 {
     if (activateUserData.IsValid())
         return new Return<DataContracts.ActivateUserData>(io.Constants.SUCCESS, "", "", activateUserData);
     else
         return new Return<DataContracts.ActivateUserData>(io.Constants.FAILURE, "", "", activateUserData);
 }
        internal static io.Data.Return<DataContracts.ForgotPasswordData> ForgotPassword(DataContracts.ForgotPasswordData forgotPasswordData)
        {
            const string functionName = _className + ".ForgotPassword()";

            if (!forgotPasswordData.IsValid())
                return new io.Data.Return<DataContracts.ForgotPasswordData>(io.Constants.FAILURE, "Invalid Email", "", forgotPasswordData);

            string where = "Email = '" + forgotPasswordData.Email.Value + "'";

            using (var rows = new iocontacts.Databases.io_contacts.Tables.EntityContacts(where, string.Empty))
            {
                if (rows.QueryResult.Failed)
                    return new io.Data.Return<DataContracts.ForgotPasswordData>(io.Constants.SUCCESS, "", "", forgotPasswordData);

                if (rows.Count == 0)
                    return new io.Data.Return<DataContracts.ForgotPasswordData>(io.Constants.SUCCESS, "", "", forgotPasswordData);

                string guid = rows[0].UID;

                rows[0].UID = Guid.NewGuid().ToString();

                if (rows[0].UpdateRow().Success)
                    guid = rows[0].UID;

                string rootSite = Common.SiteRoot();

                Email.SendEmail(0, rows[0].Email, "Forgot Password", "Click " + rootSite + "/changepassword.aspx?t=" + guid + " to change password.");

                return new io.Data.Return<DataContracts.ForgotPasswordData>(io.Constants.SUCCESS, "", "", forgotPasswordData);
            }
        }
        internal static io.Data.Return<DataContracts.CredentialData> Login(DataContracts.CredentialData credentialData)
        {
            var functionName = "ioauth.Modules.Authentication.Authenticate.Login()";

            if (!credentialData.IsValid())
                return new io.Data.Return<DataContracts.CredentialData>(io.Constants.FAILURE, @"Invalid Login Id or Password", "", credentialData);

            using (var users = new iocontacts.Databases.io_contacts.Views.DataContracts.Login.GetUser(credentialData.Email.Value))
            {
                if (users.QueryResult.Failed)
                    users.QueryResult.LogResult(1, 1, 1, 0, 101, functionName);

                if (users.Count ==0)
                    return new io.Data.Return<DataContracts.CredentialData>(io.Constants.FAILURE, @"Invalid Login Id or Password", "", credentialData);

                if (users[0].Password != GenerateHash(credentialData.Password.Value, users[0].EntityContactKey.ToString()))
                    return new io.Data.Return<DataContracts.CredentialData>(io.Constants.FAILURE, @"Invalid Login Id or Password", "", credentialData);

                if (users[0].Active == false)
                    return new io.Data.Return<DataContracts.CredentialData>(io.Constants.FAILURE, @"Account not active", "", credentialData);

                using (var sessions = new GetActiveSessions(credentialData.UserAgent.Value, users[0].EntityContactKey))
                {
                    foreach (GetActiveSessions.ActiveSession session in sessions)
                        session.Active = false;

                    credentialData.FirstName.Value = users[0].FirstName;
                    credentialData.LastName.Value = users[0].LastName;
                    credentialData.Email.Value = users[0].Email;

                    GetActiveSessions.ActiveSession newSession = null;
                    newSession = sessions.NewActiveSession();
                    newSession.EntityContactKey = users[0].EntityContactKey;
                    newSession.Active = true;
                    newSession.LastActivity = DateTime.Now.ToString();
                    newSession.UserAgent = credentialData.UserAgent.Value.ToString();

                    io.Data.Return<bool> updateResult = sessions.Update();

                    if (updateResult.Failed)
                        return new io.Data.Return<DataContracts.CredentialData>(io.Constants.FAILURE, "Failed to create session", updateResult.Message, credentialData).LogResult(1, 1, 1, 0, 100, functionName);

                    var newSessions = Databases.io_auth.Tables.UserSessions.GetObjectWithKey(newSession.UserSessionKey);
                    if (newSessions.Failed)
                        return new io.Data.Return<DataContracts.CredentialData>(io.Constants.FAILURE, "Failed to create session", updateResult.Message, credentialData).LogResult(1, 1, 1, 0, 100, functionName);

                    credentialData.Token.Value = newSessions.Value.Token;
                    credentialData.Token.IsValid = true;

                    return new io.Data.Return<DataContracts.CredentialData>(io.Constants.SUCCESS, "", "", credentialData);
                }
            }
        }
        internal static io.Data.Return<DataContracts.UpdatePasswordData> ChangePassword(UserSession userSession, DataContracts.UpdatePasswordData passwordData)
        {
            if (!passwordData.IsValid())
                return new io.Data.Return<DataContracts.UpdatePasswordData>(io.Constants.FAILURE, "Invalid Password", "", passwordData);

            if (passwordData.NewPassword.Value != passwordData.RepeatPassword.Value)
                return new io.Data.Return<DataContracts.UpdatePasswordData>(io.Constants.FAILURE, "New Password must match", "", passwordData);

            var updatePassword = iocontacts.Modules.Administration.EntityContact.SetPassword(userSession.UserSessionKey, userSession.EntityContactKey, passwordData.NewPassword.Value, passwordData.OldPassword.Value, true);

            if (updatePassword.Failed)
                return new io.Data.Return<DataContracts.UpdatePasswordData>(io.Constants.FAILURE, updatePassword.Message, "", passwordData);

            return new io.Data.Return<DataContracts.UpdatePasswordData>(io.Constants.SUCCESS, updatePassword.Message, "", passwordData);
        }
        internal static io.Data.Return<DataContracts.ResetPasswordData> ResetPassword(DataContracts.ResetPasswordData passwordData)
        {
            const string functionName = _className + ".ResetPassword()";

            if (!passwordData.IsValid())
                return new io.Data.Return<DataContracts.ResetPasswordData>(io.Constants.FAILURE, "Check required fields", "", passwordData);

            Guid validGUID;
            if (!Guid.TryParse(passwordData.UID.Value.ToString(), out validGUID))
                return new io.Data.Return<DataContracts.ResetPasswordData>(io.Constants.FAILURE, "Check required fields", "", passwordData);

            if (passwordData.NewPassword.Value != passwordData.RepeatPassword.Value)
                return new io.Data.Return<DataContracts.ResetPasswordData>(io.Constants.FAILURE, "New Password must match", "", passwordData);

            var where = "(UID = '" + validGUID.ToString() + "')";
            int entityContactKey = 20;

            using (var rows = new Databases.io_contacts.Tables.EntityContacts(where, ""))
            {
                if (rows.QueryResult.Failed)
                    return new io.Data.Return<DataContracts.ResetPasswordData>(io.Constants.FAILURE, "Unable to change password, contact Administrator.", rows.QueryResult.Message, passwordData).LogResult(Constants.SystemInstallKey, Constants.SystemKey, Constants.AppKey, 0, (int)ErrorCodes.QueryContactFailed, functionName);

                if (rows.Count == 0)
                    return new io.Data.Return<DataContracts.ResetPasswordData>(io.Constants.FAILURE, "Unable to change password, contact Administrator.", rows.QueryResult.Message, passwordData).LogResult(Constants.SystemInstallKey, Constants.SystemKey, Constants.AppKey, 0, (int)ErrorCodes.QueryContactFailed, functionName);

                entityContactKey = rows[0].EntityContactKey;
            }

            var updatePassword = iocontacts.Modules.Administration.EntityContact.SetPassword(0, entityContactKey, passwordData.NewPassword.Value, passwordData.RepeatPassword.Value, false);

            if (updatePassword.Failed)
                return new io.Data.Return<DataContracts.ResetPasswordData>(io.Constants.FAILURE, updatePassword.Message, "", passwordData);

            return new io.Data.Return<DataContracts.ResetPasswordData>(io.Constants.SUCCESS, updatePassword.Message, "", passwordData);
        }