Exemplo n.º 1
0
        public ActionResult DatabaseAuthentication(AllModels model)
        {
            string ConnectionString = Server.MapPath("~/DatabaseConnection/ConnectionString.txt");
            string Username         = Server.MapPath("~/DatabaseConnection/Username.txt");
            string Password         = Server.MapPath("~/DatabaseConnection/Password.txt");

            string AdminUser = Server.MapPath("~/DatabaseConnection/AdminUser.txt");
            string AdminPass = Server.MapPath("~/DatabaseConnection/AdminPassword.txt");

            string AdminUserName = System.IO.File.ReadAllText(AdminUser);
            string AdminPassword = System.IO.File.ReadAllText(AdminPass);

            string User2 = EncryptionAndDecryption.Decrypt(AdminUserName);
            string Pass2 = EncryptionAndDecryption.Decrypt(AdminPassword);

            if (model.Maintenance.AdminUser != User2.ToString().TrimEnd() && model.Maintenance.AdminPassword != Pass2.ToString().TrimEnd())
            {
                ViewBag.HeaderError  = string.Format("Unable  to connect to server.");
                ViewBag.MessageError = string.Format("Check your Admin Username or Admin Password.");
                return(View());
            }


            using (StreamWriter sw = System.IO.File.CreateText(ConnectionString))
            {
                sw.WriteLine(model.Maintenance.DatabaseConnection);
            }

            using (StreamWriter sw = System.IO.File.CreateText(Username))
            {
                sw.WriteLine(EncryptionAndDecryption.Encrypt(model.Maintenance.DatabaseUser));
            }

            using (StreamWriter sw = System.IO.File.CreateText(Password))
            {
                sw.WriteLine(EncryptionAndDecryption.Encrypt(model.Maintenance.DatabasePassword));
            }

            DBConnection();

            using (SqlConnection connection = new SqlConnection(con))
            {
                try
                {
                    connection.Open();
                }
                catch
                {
                    ViewBag.HeaderError  = string.Format("Unable  to connect to server.");
                    ViewBag.MessageError = string.Format("Check your server connection.");
                    return(View());
                }
            }

            ViewBag.HeaderSuccess  = string.Format("Connection Success.");
            ViewBag.MessageSuccess = string.Format("Your Connection Server has connected.");
            return(View());
        }