public static IApplication GetRemotehMailServerApplication()
        {
            hMailServerNetRemote.IClassFactory cf = RemoteActivation.GetRemoteClassFactory();

            hMailServerNetRemote.IApplication app;
            if (HttpContext.Current.Session["hMailServerNetRemoteApplication"] == null)
            {
                app = cf.CreateApplication();
                HttpContext.Current.Session["hMailServerNetRemoteApplication"] = app;
            }
            else
            {
                app = (hMailServerNetRemote.IApplication)HttpContext.Current.Session["hMailServerNetRemoteApplication"];
            }

            return(app);
        }
        //
        // MembershipProvider.ValidateUser
        //

        public override bool ValidateUser(string username, string password)
        {
            bool isValid = false;


            hMailServerNetRemote.IApplication app     = RemoteActivation.GetRemotehMailServerApplication();
            hMailServerNetRemote.IAccount     account = app.Authenticate(username, password);

            if (account != null)
            {
                isValid = true;
            }
            else
            {
                UpdateFailureCount(username, "password");
            }

            return(isValid);
        }
        //
        // System.Web.Security.MembershipProvider methods.
        //

        //
        // MembershipProvider.ChangePassword
        //

        public override bool ChangePassword(string username, string oldPwd, string newPwd)
        {
            if (!ValidateUser(username, oldPwd))
            {
                return(false);
            }

            ValidatePasswordEventArgs args =
                new ValidatePasswordEventArgs(username, newPwd, true);

            OnValidatingPassword(args);

            if (args.Cancel)
            {
                if (args.FailureInformation != null)
                {
                    throw args.FailureInformation;
                }
                else
                {
                    throw new MembershipPasswordException("Change password canceled due to new password validation failure.");
                }
            }

            string[] sp = username.Split(new Char[] { '@' });

            if (sp.Length != 2)
            {
                return(false);
            }

            hMailServerNetRemote.IApplication app     = RemoteActivation.GetRemotehMailServerApplication();
            hMailServerNetRemote.IDomain      domain  = app.Domains.ItemByName(sp[1]);
            hMailServerNetRemote.IAccount     account = domain.Accounts[username];
            account.Password = newPwd;
            account.Save();

            return(true);
        }
        //
        // GetUserFromReader
        //    A helper function that takes the current row from the MySqlDataReader
        // and hydrates a MembershiUser from the values. Called by the
        // MembershipUser.GetUser implementation.
        //

        private hMailServerNetRemote.IAccount GetAccount(string email)
        {
            hMailServerNetRemote.IApplication app = RemoteActivation.GetRemotehMailServerApplication();
            string[] sp = email.Split(new Char[] { '@' });

            if (sp.Length != 2)
            {
                return(null);
            }

            hMailServerNetRemote.IDomain domain = app.Domains.ItemByName(sp[1]);
            if (domain == null)
            {
                return(null);
            }

            hMailServerNetRemote.IAccount account = domain.Accounts.ItemByAddress(email);
            if (account == null)
            {
                return(null);
            }

            return(account);
        }