示例#1
0
        //改
        public bool CheckUser(MyWebDB myWebDB, FormData formData)
        {
            int result      = 0;
            var queryResult = from item in myWebDB.User
                              select new { name = item.UserName, password = item.Password };

            foreach (var i in queryResult)
            {
                if (i.name.Trim() == formData.name)
                {
                    if (i.password.Trim() == formData.password)
                    {
                        result++;
                    }
                }
            }
            if (result > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#2
0
 public IActionResult Comment(string text)
 {
     try
     {
         using (MyWebDB DB = new MyWebDB())
         {
             if (User.Identity.IsAuthenticated)
             {
                 var comment = new CommentDoNet
                 {
                     Name        = User.Identity.Name,
                     CommentText = text.Trim(),
                     Date        = DateTime.Now.Date,
                     Time        = DateTime.Now.TimeOfDay
                 };
                 services.AddComment(DB, admRpo, userRpo, comment);
             }
             else
             {
                 var comment = new CommentDoNet
                 {
                     Name        = "UnknownUser",
                     CommentText = text.Trim(),
                     Date        = DateTime.Now.Date,
                     Time        = DateTime.Now.TimeOfDay
                 };
                 services.AddComment(DB, admRpo, userRpo, comment);
             }
         }
         return(RedirectToAction(nameof(UpController.Comment), "Up"));
     }catch
     {
         return(RedirectToAction(nameof(HomeController.Introduction), "Home"));
     }
 }
示例#3
0
        public IActionResult Comment()
        {
            List <CommentDoNet> commentDoNets = new List <CommentDoNet>();

            using (MyWebDB DB = new MyWebDB())
            {
                commentDoNets = userRpo.ShowAllComment(DB);
            }
            return(View(commentDoNets));
        }
示例#4
0
 public bool ShowAllComment(MyWebDB dB, IRepository <AdministratorDoNet> admRpo, IRepository <UserDoNet> userRpo)
 {
     try
     {
         userRpo.ShowAllComment(dB);
         return(true);
     }catch
     {
         return(false);
     }
 }
示例#5
0
 public bool AddComment(MyWebDB dB, IRepository <AdministratorDoNet> admRpo, IRepository <UserDoNet> userRpo, CommentDoNet commentDoNet)
 {
     try
     {
         userRpo.AddCommentDoNet(dB, commentDoNet);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
示例#6
0
 public bool AddCommentDoNet(MyWebDB myWebDB, CommentDoNet commentDoNet)
 {
     try
     {
         myWebDB.Comment.Add(commentDoNet);
         myWebDB.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
示例#7
0
        public async Task <IActionResult> Login(FormData formData)
        {
            if (formData.name == null || formData.password == null)
            {
                ViewData["Message"] = "登陆信息不能为空!";
                return(View(nameof(HomeController.Message), "Account"));
            }
            else
            {
                using (MyWebDB DB = new MyWebDB())
                {
                    if ((int)services.Login(DB, admRpo, userRpo, formData) == 0)
                    {
                        var ClaimsIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                        ClaimsIdentity.AddClaim(new Claim(ClaimTypes.Name, formData.name));
                        ClaimsIdentity.AddClaim(new Claim(ClaimTypes.Role, "User"));
                        ClaimsPrincipal user = new ClaimsPrincipal(ClaimsIdentity);
                        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                                      user, new AuthenticationProperties()
                        {
                            IsPersistent = true,
                            ExpiresUtc   = DateTimeOffset.UtcNow.AddMinutes(10),
                            AllowRefresh = true
                        });

                        return(RedirectToAction(nameof(AccountController.UserController), "Account"));
                    }
                    else if ((int)services.Login(DB, admRpo, userRpo, formData) == 1)
                    {
                        var ClaimsIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                        ClaimsIdentity.AddClaim(new Claim(ClaimTypes.Name, formData.name));
                        ClaimsIdentity.AddClaim(new Claim(ClaimTypes.Role, "Administrator"));
                        ClaimsPrincipal user = new ClaimsPrincipal(ClaimsIdentity);
                        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                                      user, new AuthenticationProperties()
                        {
                            IsPersistent = true,
                            ExpiresUtc   = DateTimeOffset.UtcNow.AddMinutes(10),
                            AllowRefresh = true
                        });

                        return(RedirectToAction(nameof(AccountController.AdministratorController), "Account"));
                    }
                    else
                    {
                        ViewData["Message"] = "登陆失败!该用户不存在!";
                        return(View(nameof(HomeController.Message), "Account"));
                    }
                }
            }
        }
示例#8
0
 public bool AddUser(MyWebDB myWebDB, FormData formData)
 {
     try
     {
         var AdmDoNet = new AdministratorDoNet
         {
             AdministratorName = formData.name,
             Password          = formData.password,
             IsUser            = false
         };
         myWebDB.Administrator.Add(AdmDoNet);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
示例#9
0
 public bool AddUser(MyWebDB myWebDB, FormData formData)
 {
     try
     {
         var userDoNet = new UserDoNet
         {
             UserName = formData.name,
             Password = formData.password,
             IsUser   = true
         };
         myWebDB.User.Add(userDoNet);
         myWebDB.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
示例#10
0
 public bool Register(MyWebDB dB, IRepository <AdministratorDoNet> admRpo, IRepository <UserDoNet> userRpo, FormData formData)
 {
     try
     {
         if (userRpo.CheckUserByName(dB, formData.name))
         {
             return(false);
         }
         else
         {
             userRpo.AddUser(dB, formData);
             return(true);
         }
     }
     catch
     {
         return(false);
     }
 }
示例#11
0
 public IActionResult Register(FormData formData)
 {
     if (formData.name == null || formData.password == null)
     {
         ViewData["Message"] = "注册信息不能为空!";
         return(View(nameof(HomeController.Message), "Account"));
     }
     using (MyWebDB DB = new MyWebDB())
     {
         if (services.Register(DB, admRpo, userRpo, formData))
         {
             ViewData["Message"] = "注册成功!";
             return(View(nameof(HomeController.Message), "Account"));
         }
         else
         {
             ViewData["Message"] = "注册失败!用户名已存在";
             return(View(nameof(HomeController.Message), "Account"));
         }
     }
 }
示例#12
0
        public IActionResult Introduction()
        {
            List <IdentityInfo> identityInfos = new List <IdentityInfo>();

            using (MyWebDB DB = new MyWebDB())
            {
                foreach (var adm in admRpo.GetAllUser(DB))
                {
                    identityInfos.Add(new IdentityInfo {
                        name = adm.AdministratorName, IsUser = false
                    });
                }
                foreach (var user in userRpo.GetAllUser(DB))
                {
                    identityInfos.Add(new IdentityInfo {
                        name = user.UserName, IsUser = true
                    });
                }
            }
            return(View(identityInfos));
        }
示例#13
0
 public Identity Login(MyWebDB dB, IRepository <AdministratorDoNet> admRpo, IRepository <UserDoNet> userRpo, FormData formData)
 {
     try
     {
         if (userRpo.CheckUser(dB, formData))
         {
             return(Identity.user);
         }
         else if (admRpo.CheckUser(dB, formData))
         {
             return(Identity.administrator);
         }
         else
         {
             return(Identity.none);
         }
     }catch
     {
         return(Identity.none);
     }
 }
示例#14
0
        public bool CheckUserByName(MyWebDB myWebDB, string name)
        {
            int result      = 0;
            var queryResult = from item in myWebDB.User
                              select new { name = item.UserName };

            foreach (var i in queryResult)
            {
                if (i.name.Trim() == name)
                {
                    result++;
                }
            }
            if (result > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#15
0
 //删
 public async Task <bool> EditUser(MyWebDB myWebDB, FormData formData)
 {
     try
     {
         var expr = from item in myWebDB.User
                    where item.UserName == formData.name
                    select item;
         var UserEntity = expr.SingleOrDefault();
         if (UserEntity == null)
         {
             return(false);
         }
         UserEntity.UserName = formData.name;
         UserEntity.Password = formData.password;
         myWebDB.Set <UserDoNet>().Update(UserEntity);
         return(await myWebDB.SaveChangesAsync() > 0);
     }
     catch
     {
         return(false);
     }
 }
示例#16
0
 //增
 public async Task <bool> DeleteUser(MyWebDB myWebDB, FormData formData)
 {
     try
     {
         var expr = from item in myWebDB.User
                    where item.UserName == formData.name
                    select item;
         var UserEntity = expr.SingleOrDefault();
         if (UserEntity == null)
         {
             return(false);
         }
         else
         {
             myWebDB.Set <UserDoNet>().Remove(UserEntity);
             return(await myWebDB.SaveChangesAsync() > 0);
         }
     }
     catch
     {
         return(false);
     }
 }
示例#17
0
 //查
 public List <AdministratorDoNet> GetAllUser(MyWebDB myWebDB)
 {
     return(myWebDB.Administrator.ToList <AdministratorDoNet>());
 }
示例#18
0
 public bool CheckUserByName(MyWebDB myWebDB, string name)
 {
     throw new NotImplementedException();
 }
示例#19
0
 public List <CommentDoNet> ShowAllComment(MyWebDB myWebDB)
 {
     throw new NotImplementedException();
 }
示例#20
0
 public List <CommentDoNet> ShowAllComment(MyWebDB myWebDB)
 {
     return(myWebDB.Comment.ToList <CommentDoNet>());
 }
示例#21
0
        //查

        public List <UserDoNet> GetAllUser(MyWebDB myWebDB)
        {
            return(myWebDB.User.ToList <UserDoNet>());
        }