Пример #1
0
 public IActionResult Login(User user)
 {
     if (ModelState.IsValid)
     {
         if (user.UserName == "admin" && user.UserPad == "123456")
         {
             var claims = new[] {
                 new Claim(ClaimTypes.Name, "admin")
                 , new Claim(ClaimTypes.Role, "admin")
             };
             ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme));
             HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties {
             }).Wait();
             if (claimsPrincipal.Identity.IsAuthenticated)
             {
                 return(Redirect("/admin/index"));
             }
         }
         else
         {
             var ems = _employee.GetAllEmployees();
             foreach (var i in ems)
             {
                 if (i.UserName == user.UserName && i.Passward == user.UserPad)
                 {
                     var claims = new[] {
                         new Claim(ClaimTypes.Name, i.UserName)
                         , new Claim(ClaimTypes.Role, "user")
                         , new Claim(ClaimTypes.Sid, i.EmpId)
                     };
                     ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme));
                     HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal).Wait();
                     if (claimsPrincipal.Identity.IsAuthenticated)
                     {
                         return(RedirectToAction("index", "user", new { id = i.EmpId }));
                     }
                 }
             }
         }
     }
     ModelState.AddModelError("UserName", "用户名或密码错误");
     return(View("Login"));
 }
        public IActionResult BackUp()
        {
            string path1 = AppDomain.CurrentDomain.BaseDirectory + "employee.txt";
            string path2 = AppDomain.CurrentDomain.BaseDirectory + "department.txt";
            string path3 = AppDomain.CurrentDomain.BaseDirectory + "movedep.txt";
            string path4 = AppDomain.CurrentDomain.BaseDirectory + "apply.txt";

            //判断文件是否存在,没有则创建。
            if (!System.IO.File.Exists(path1))
            {
                FileStream stream = System.IO.File.Create(path1);
                stream.Close();
                stream.Dispose();
            }
            if (!System.IO.File.Exists(path2))
            {
                FileStream stream = System.IO.File.Create(path2);
                stream.Close();
                stream.Dispose();
            }
            if (!System.IO.File.Exists(path3))
            {
                FileStream stream = System.IO.File.Create(path3);
                stream.Close();
                stream.Dispose();
            }
            if (!System.IO.File.Exists(path4))
            {
                FileStream stream = System.IO.File.Create(path4);
                stream.Close();
                stream.Dispose();
            }

            //写入日志
            using (StreamWriter writer = new StreamWriter(path1, false))
            {
                var ems = _employeeSql.GetAllEmployees();
                foreach (var i in ems)
                {
                    writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", i.EmpId, i.Name, i.DepId, i.State, i.UserName, i.Passward);
                }
            }
            using (StreamWriter writer = new StreamWriter(path2, false))
            {
                var k = _departmentSql.GetAllDepartments();
                foreach (var i in k)
                {
                    writer.WriteLine("{0}\t{1}", i.Name, i.DepId);
                }
            }
            using (StreamWriter writer = new StreamWriter(path3, false))
            {
                var k = _moveDelSql.GetAllMoveDeps();
                foreach (var i in k)
                {
                    writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", i.Id, i.EmpId, i.Day, i.DepForm, i.DepTo, i.Reason);
                }
            }
            using (StreamWriter writer = new StreamWriter(path4, false))
            {
                var k = _applySql.AllApply();
                foreach (var i in k)
                {
                    writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", i.Id, i.EmpId, i.StateChange, i.Reason, i.IsSure, i.IsAgree);
                }
            }

            return(Content("<script>alert('备份成功!'); history.go(-1);</script>", "text/html;charset=utf-8"));
        }