示例#1
0
        public ActionResult Login(Login l, string ReturnUrl = "")
        {
            if (ModelState.IsValid)
            {
                var  hashedPass             = FormsAuthentication.HashPasswordForStoringInConfigFile(l.Person_Password, "MD5");
                bool isValidUser            = Membership.ValidateUser(l.Person_ID, hashedPass);
                LibraryAssistantEntities db = new LibraryAssistantEntities();
                if (isValidUser)
                {
                    Registered_Person registered_person = null;
                    registered_person = db.Registered_Person.Where(a => a.Person_ID.Equals(l.Person_ID)).FirstOrDefault();
                    if (registered_person != null)
                    {
                        //initiate an instance of a passable registered student
                        Registered_Person passablePerson = new Registered_Person();
                        passablePerson.Person_ID       = registered_person.Person_ID;
                        passablePerson.Person_Name     = registered_person.Person_Name;
                        passablePerson.Person_Surname  = registered_person.Person_Surname;
                        passablePerson.Person_Email    = registered_person.Person_Email;
                        passablePerson.Person_Password = registered_person.Person_Password;

                        JavaScriptSerializer js          = new JavaScriptSerializer();
                        string data                      = js.Serialize(passablePerson);
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, registered_person.Person_ID, DateTime.Now, DateTime.Now.AddMinutes(20), false, data);
                        string     encToken              = FormsAuthentication.Encrypt(ticket);
                        HttpCookie authCookies           = new HttpCookie(FormsAuthentication.FormsCookieName, encToken);
                        Response.Cookies.Add(authCookies);

                        Person_Session_Log newSession = new Person_Session_Log();

                        newSession.Person_ID       = l.Person_ID;
                        newSession.Login_DateTime  = DateTime.Now;
                        newSession.Logout_DateTime = newSession.Login_DateTime.AddMinutes(20);
                        db.Person_Session_Log.Add(newSession);

                        db.SaveChanges();

                        Session["loginSession"] = newSession;

                        if (ReturnUrl != "")
                        {
                            return(Redirect(ReturnUrl));
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                }
                else
                {
                    TempData["Message"]    = "Invalid Login Details!";
                    TempData["classStyle"] = "danger";
                    return(View());
                }
            }
            ModelState.Remove("Person_Password");
            return(View());
        }
示例#2
0
        public JsonResult BackupDatabase()
        {
            try
            {
                string dataTime   = DateTime.Now.ToString("yyyy-MM-dd") + "-" + DateTime.Now.ToString("HH-mm");
                string fileName   = "LibraryAssistant_" + dataTime + ".bak";
                string sqlCommand = @"BACKUP DATABASE [{0}] TO  DISK = N'{1}' WITH NOFORMAT, NOINIT,  NAME = N'LibraryAssistant-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10";

                using (var db = new LibraryAssistantEntities())
                {
                    string dbname = db.Database.Connection.Database;
                    db.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, string.Format(sqlCommand, dbname, fileName));
                }
                return(Json(true, JsonRequestBehavior.AllowGet));
            }
            catch
            {
                return(Json(false, JsonRequestBehavior.AllowGet));
            }
        }
示例#3
0
 public JsonResult RestoreDatabase(string path)
 {
     try
     {
         JavaScriptSerializer js = new JavaScriptSerializer();
         string _path            = (string)js.Deserialize(path, typeof(string));
         string extension        = Path.GetExtension(_path);
         if (extension != ".bak")
         {
             return(Json("extension", JsonRequestBehavior.AllowGet));
         }
         if (_path.Contains(("Log")))
         {
             return(Json("log", JsonRequestBehavior.AllowGet));
         }
         if (!_path.Contains("LibraryAssistant_"))
         {
             return(Json("library", JsonRequestBehavior.AllowGet));
         }
         //string fileName = _path.Substring(path.LastIndexOf("\\"));
         string fileName   = Path.GetFileName(_path);
         string dataTime   = DateTime.Now.ToString("yyyy-MM-dd") + "-" + DateTime.Now.ToString("HH-mm");
         string logName    = "LibraryAssistant_LogBackup_" + dataTime + ".bak";
         var    sqlCommand = @"USE [master] ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE BACKUP LOG [{0}] TO  DISK = N'{1}' WITH NOFORMAT, NOINIT,  NAME = N'LibraryAssistant_LogBackup', NOSKIP, NOREWIND, NOUNLOAD,  NORECOVERY ,  STATS = 5 RESTORE DATABASE[{0}] FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\{2}' WITH FILE = 1, NOUNLOAD, STATS = 5 ALTER DATABASE [{0}] SET MULTI_USER";
         using (var db = new LibraryAssistantEntities())
         {
             string dbname = db.Database.Connection.Database;
             db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, string.Format(sqlCommand, dbname, logName, fileName));
         }
         return(Json(true, JsonRequestBehavior.AllowGet));
     }
     catch
     {
         return(Json(false, JsonRequestBehavior.AllowGet));
     }
 }
示例#4
0
        void Session_End(object sender, EventArgs e)
        {
            Application.Lock();
            var online = (int)Application["OnlineUsers"];

            Application["OnlineUsers"] = online - 1;
            Application.UnLock();

            try
            {
                LibraryAssistantEntities db = new LibraryAssistantEntities();
                //update session end time
                var session = db.Person_Session_Log.Where(p => p.Person_ID == User.Identity.Name).OrderByDescending(d => d.Login_DateTime).First();
                if (session.Logout_DateTime == session.Login_DateTime.AddMinutes(20))
                {
                    session.Logout_DateTime = DateTime.Now;
                    db.Entry(session).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
            }
            catch
            {
            }
        }