예제 #1
0
        public ActionResult Logoff()
        {
            DateTime   someDateTime = DateTime.Now;
            HttpCookie cookieReq    = Request.Cookies["cookieAuth"];
            string     cookieString = null;

            if (cookieReq != null)
            {
                cookieString = CryporEngine.Decrypt(cookieReq["Login"], true);
                System.Diagnostics.Debug.WriteLine(cookieString);

                HttpCookie cookie = new HttpCookie("cookieAuth");
                cookie.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Add(cookie);
                if (ModelState.IsValid)
                {
                    // поиск пользователя в бд
                    User user = null;
                    using (ApplicationContext db = new ApplicationContext())
                    {
                        user = db.SetUser.Where(u => u.Login == cookieString).First();
                    }
                    if (user != null)
                    {
                        using (ApplicationContext db = new ApplicationContext())
                        {
                            SessionModel date = db.SetSession.FirstOrDefault(u => u.UserId == user.Id && u.Date == someDateTime.Date);
                            if (date != null)
                            {
                                if (date.TimeUp == date.SetTimeUp)
                                {
                                    SessionModel UPSetTimeUp = db.SetSession.Where(u => u.UserId == user.Id && u.Date == someDateTime.Date).First();
                                    UPSetTimeUp.TimeOut         = someDateTime.ToLongTimeString();
                                    UPSetTimeUp.AllTime         = Convert.ToDateTime((DateTime.Parse(UPSetTimeUp.AllTime) + (DateTime.Parse(DateTime.Now.ToLongTimeString()) - DateTime.Parse(UPSetTimeUp.TimeUp))).ToString()).ToLongTimeString();
                                    UPSetTimeUp.StatusTime      = 0;
                                    db.Entry(UPSetTimeUp).State = EntityState.Modified;
                                }
                                else
                                {
                                    SessionModel UPSetTimeUp = db.SetSession.Where(u => u.UserId == user.Id && u.Date == someDateTime.Date).First();
                                    UPSetTimeUp.TimeOut         = someDateTime.ToShortTimeString();
                                    UPSetTimeUp.AllTime         = Convert.ToDateTime((DateTime.Parse(UPSetTimeUp.AllTime) + (DateTime.Parse(DateTime.Now.ToLongTimeString()) - DateTime.Parse(UPSetTimeUp.SetTimeUp))).ToString()).ToLongTimeString();
                                    UPSetTimeUp.StatusTime      = 0;
                                    db.Entry(UPSetTimeUp).State = EntityState.Modified;
                                }
                                db.SaveChanges();
                            }
                        }
                    }
                }
                FormsAuthentication.SignOut();
            }
            return(RedirectToAction("_Index", "Home"));
        }
        public ActionResult Login(LoginModel model)
        {
            string someDateTime = DateTime.Now.ToShortDateString();

            if (ModelState.IsValid)
            {
                // поиск пользователя в бд
                User user = null;
                using (ApplicationContext db = new ApplicationContext())
                {
                    string password = CodePass(model.Password);
                    user = db.SetUser.FirstOrDefault(u => u.Login == model.Login && u.Password == password);
                }
                if (user != null)
                {
                    SessionHubModel date = null;
                    using (ApplicationContext db = new ApplicationContext())
                    {
                        date = db.SetSessionHubModel.FirstOrDefault(u => u.UserId == user.Id && u.Date == someDateTime && u.IsAction == true);
                    }
                    if (date == null)
                    {
                        FormsAuthentication.SetAuthCookie(model.Login, true);
                        // Создать объект cookie-набора
                        HttpCookie cookie = new HttpCookie("cookieAuth");

                        // Установить значения в нем
                        cookie["Login"] = CryporEngine.Encrypt(model.Login, true);
                        cookie.Expires  = DateTime.Now.AddYears(1);

                        // Добавить куки в ответ
                        Response.Cookies.Add(cookie);

                        return(RedirectToAction("_Index", "Home"));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Пользователь Авторизирован");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Пользователя с таким логином и паролем нет");
                }
            }

            return(View(model));
        }
예제 #3
0
        public void Logofff()
        {
            DateTime   someDateTime = DateTime.Now;
            HttpCookie cookieReq    = Request.Cookies["cookieAuth"];
            string     cookieString = null;

            if (cookieReq != null)
            {
                cookieString = CryporEngine.Decrypt(cookieReq["Login"], true);
            }

            System.Diagnostics.Debug.WriteLine(cookieString);

            HttpCookie cookie = new HttpCookie("cookieAuth");

            cookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(cookie);

            FormsAuthentication.SignOut();
        }