Example #1
0
        public ActionResult Login(AdminLoginViewModel model, string returnUrl)
        {
            var admin = accountService.AdminLogin(model.Account, model.Password);

            if (admin != null)
            {
                LocalDateTimeService timeService = new LocalDateTimeService();
                var today = timeService.GetLocalDateTime(LocalDateTimeService.CHINA_STANDARD_TIME);
                var name  = admin.Adm_Name;
                HttpContext.Session.Clear();
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                                                                                 name,
                                                                                 today,
                                                                                 DateTime.Now.AddHours(24),
                                                                                 false,
                                                                                 "Admin"
                                                                                 );

                string enTicket = FormsAuthentication.Encrypt(ticket);
                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, enTicket));
                return(Redirect("/Admin/Soap"));
            }

            TempData["loginFail"] = "帳號或密碼錯誤!";
            return(View());
        }
        public override int SaveChanges()
        {
            try
            {
                var modifiedEntries = ChangeTracker.Entries()
                                      .Where(x => x.Entity is AuditableEntity &&
                                             (x.State == System.Data.Entity.EntityState.Added || x.State == System.Data.Entity.EntityState.Modified));

                foreach (var entry in modifiedEntries)
                {
                    AuditableEntity entity = entry.Entity as AuditableEntity;
                    if (entity != null)
                    {
                        string identityName      = Thread.CurrentPrincipal.Identity.Name;
                        LocalDateTimeService obj = new LocalDateTimeService();
                        DateTime             now = obj.GetDateTime();

                        if (entry.State == System.Data.Entity.EntityState.Added)
                        {
                            entity.CreatedBy = identityName;
                            entity.CreatedOn = now;
                        }
                        else if (entry.State == System.Data.Entity.EntityState.Modified)
                        {
                            entity.UpdatedBy = identityName;
                            entity.UpdatedOn = now;
                        }
                        else
                        {
                            base.Entry(entity).Property(x => x.CreatedBy).IsModified = false;
                            base.Entry(entity).Property(x => x.CreatedOn).IsModified = false;
                            entity.UpdatedBy = identityName;
                            entity.UpdatedOn = now;
                        }
                    }
                }
                return(base.SaveChanges());
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
            catch (SqlException ex)
            {
                throw ex;
            }
        }
Example #3
0
        public ActionResult Login(LoginViewModel vm)
        {
            var customer = accountService.Login(vm);

            if (customer != null)
            {
                LocalDateTimeService timeService = new LocalDateTimeService();
                var today = timeService.GetLocalDateTime(LocalDateTimeService.CHINA_STANDARD_TIME);
                var name  = customer.Name;
                HttpContext.Session.Clear();
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                                                                                 name,
                                                                                 today,
                                                                                 DateTime.Now.AddHours(24),
                                                                                 false,
                                                                                 "User"
                                                                                 );

                var idCookie = new HttpCookie("IdCookie");
                idCookie.Expires.AddHours(24);
                idCookie.Values.Add("customer_id", customer.Id);
                //Encrypt cookie
                string enTicket = FormsAuthentication.Encrypt(ticket);
                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, enTicket));
                Response.Cookies.Add(idCookie);

                //string decodedUrl = "";
                //if (!string.IsNullOrEmpty(returnUrl))
                //    decodedUrl = Server.UrlDecode(returnUrl);

                ////Login logic...

                //if (Url.IsLocalUrl(decodedUrl))
                //{
                //    return Redirect(decodedUrl);
                //}

                return(RedirectToAction("Index", "Home"));
            }

            ModelState.AddModelError("Password", "帳號或密碼錯誤,請重新確認");
            return(View(vm));
        }