Example #1
0
        public IActionResult Password([Required, MinLength(6)] String password, [Required] String lastPassword)
        {
            if (ModelState.IsValid)
            {
                LoginUserModel user      = _analysis.GetLoginUserModel(HttpContext);
                Principal      principal = _context.Principals.Find(user.UserId);

                if (principal != null)
                {
                    String dbLastPassword  = _encryption.DecryptByRsa(principal.Password);
                    String lastPasswordMd5 = _encryption.EncodeByMd5Times(lastPassword, 2);
                    if (lastPasswordMd5 == dbLastPassword)
                    {
                        principal.Password = _encryption.EncodeByRsa(_encryption.EncodeByMd5Times(password, 2));
                        _context.SaveChanges();
                        HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                        return(Json(new
                        {
                            isOk = true,
                            title = "提示",
                            message = $"修改成功"
                        }));
                    }
                    else
                    {
                        return(Json(new
                        {
                            isOk = false,
                            title = "错误",
                            message = $"旧密码错误!"
                        }));
                    }
                }
                else
                {
                    return(Json(new
                    {
                        isOk = false,
                        title = "错误",
                        message = $"用户不存在或者已经被删除!。 "
                    }));
                }
            }
            else
            {
                return(Json(new
                {
                    isOk = false,
                    title = "错误",
                    message = $"参数错误!输入了不合规范的参数。 "
                }));
            }
        }
Example #2
0
        public IActionResult Login([Required] String userId, [Required] String userPassword)
        {
            if (ModelState.IsValid)
            {
                userId       = userId.Trim();
                userPassword = userPassword.Trim();

                //判断用户身份
                UserType type = _analysis.GetUserType(userId);
                if (type == UserType.Anonymous) //匿名用户
                {
                    return(Json(new
                    {
                        isOk = false,
                        message = "账户不存在!如果你的账号尚未录入请联系系统维护人员录入!",
                    }));
                }
                //如果是管理员判断密码是否正确
                if (type == UserType.Principal)
                {
                    Principal principal = _context.Principals.Find(userId);
                    if (_ncryption.DecryptByRsa(principal.Password) != _ncryption.EncodeByMd5(_ncryption.EncodeByMd5(userPassword)))
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "管理员的密码不正确!",
                        }));
                    }
                }
                //如果是学生判断密码是正确
                if (type == UserType.Student)
                {
                    if (!_context.Student.Any(stu => stu.Password == _ncryption.EncodeByMd5(_ncryption.EncodeByMd5(userPassword))))
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "同学你的密码不正确!忘记了可以修改密码。",
                        }));
                    }
                }
                //判断是否让管理员登录 超级管理员不被禁止登录
                SystemSetting setting = _config.LoadSystemSetting();
                if (type == UserType.Principal)
                {
                    Principal principal = _context.Principals.Find(userId);

                    if (!setting.LoginSetting.PrincipalLogin && principal.PrincipalStatus != PrincipalStatus.Super)
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "系统维护中,管理员请等待系统维护之后进入!",
                        }));
                    }
                    //判断此管理员是否已经被禁止
                    if (principal.PrincipalStatus == PrincipalStatus.Ban)
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "管理员,你已经被禁止登录!",
                        }));
                    }

                    //验证成功保存信息让其登录
                    LoginUserModel user = new LoginUserModel()
                    {
                        UserId       = userId,
                        UserPassword = userPassword,
                        LoginTime    = DateTime.Now,
                        UserType     = type
                    };
                    var            userData = JsonConvert.SerializeObject(user, Formatting.None);
                    ClaimsIdentity identity = new ClaimsIdentity();
                    identity.AddClaim(new Claim(ClaimTypes.Name, principal.Name)); //用户名 姓名
                    identity.AddClaim(new Claim(ClaimTypes.Role, "Principal"));    //角色
                    identity.AddClaim(new Claim(ClaimTypes.UserData, userData));   //用户数据
                    ClaimsPrincipal claimPrincipal = new ClaimsPrincipal(identity);

                    AuthenticationProperties property = new AuthenticationProperties
                    {
                        ExpiresUtc   = DateTimeOffset.UtcNow.AddHours(16),//保存 16小时
                        IsPersistent = true
                    };
                    //持久化 Cookie 浏览器关闭了 只有在IsPersistent为True时,才会在写入Cookie指定Expires
                    HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimPrincipal, property);

                    return(Json(new
                    {
                        isOk = true,
                        url = "/Principal/Index",
                        message = "登录成功!"
                    }));
                }
                else
                {
                    Student student = _context.Student.Find(userId);
                    if (!setting.LoginSetting.StudentLogin)
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "系统尚未允许学生登录!请等待通知...",
                        }));
                    }

                    //模块判断
                    if (!_context.InstituteToModules.Any(im => im.InstituteId == student.InstituteId))
                    {
                        return(Json(new
                        {
                            isOk = false,
                            message = "你所在学院并没有被规划在考试模块内,你无法参与实验室安全学习...",
                        }));
                    }
                    InstituteToModule itm = _context.InstituteToModules.FirstOrDefault(m => m.InstituteId == student.InstituteId);

                    /* 学习任务安排 -- 判断是否安排了学习任务 */
                    if (!_context.VLearningMaps.Any(l => l.StudentId == student.StudentId && l.ModuleId == itm.ModuleId))
                    {
                        /* CourceView 自动统计了 每个课程的 在用的视频资源数量 */
                        List <vCourceMap> courseMaps = _context.VCourceMaps
                                                       .Where(vc => vc.ModuleId == itm.ModuleId && vc.RCount != 0 && vc.CourceStatus == CourceStatus.Using)
                                                       .ToList(); //找出在用的所有课程

                        /* 如果有学习任务*/
                        if (courseMaps.Count > 0)
                        {
                            //安排学习课程
                            foreach (var item in courseMaps)
                            {
                                Learing learning = new Learing
                                {
                                    StudentId = student.StudentId,
                                    CourceId  = item.CourceId,
                                    IsFinish  = false,
                                    AddTime   = DateTime.Now
                                };
                                _context.Learings.Add(learning);
                                //记录学习进度

                                List <Resource> resources = _context.Resources
                                                            .Where(r => r.CourceId == item.CourceId)
                                                            .Where(r => r.ResourceStatus == ResourceStatus.Using)
                                                            .Where(r => r.ResourceType == ResourceType.Vedio)
                                                            .ToList();

                                foreach (var res in resources)
                                {
                                    Progress progress = new Progress
                                    {
                                        AddTime    = DateTime.Now,
                                        NeedTime   = res.LengthOfStudy,
                                        StudyTime  = 0,
                                        StudentId  = student.StudentId,
                                        ResourceId = res.ResourceId
                                    };
                                    _context.Progresses.Add(progress);
                                }
                            }
                        }
                        /*如果没有学习任务*/
                    }
                    _context.SaveChanges();

                    LoginUserModel user = new LoginUserModel()
                    {
                        UserId       = userId,
                        UserPassword = userPassword,
                        LoginTime    = DateTime.Now,
                        UserType     = type
                    };
                    var            userData = JsonConvert.SerializeObject(user, Formatting.None);
                    ClaimsIdentity identity = new ClaimsIdentity();
                    identity.AddClaim(new Claim(ClaimTypes.Name, student.Name)); //用户名 姓名
                    identity.AddClaim(new Claim(ClaimTypes.Role, "Student"));    //角色
                    identity.AddClaim(new Claim(ClaimTypes.UserData, userData)); //用户数据
                    ClaimsPrincipal claimPrincipal = new ClaimsPrincipal(identity);

                    AuthenticationProperties property = new AuthenticationProperties
                    {
                        ExpiresUtc   = DateTimeOffset.UtcNow.AddHours(28),//保存28小时
                        IsPersistent = true
                    };
                    //持久化 Cookie 浏览器关闭了 只有在IsPersistent为True时,才会在写入Cookie指定Expires
                    HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimPrincipal, property);

                    return(Json(new
                    {
                        isOk = true,
                        url = "/Student/Index",
                        message = "登录成功!"
                    }));
                }
            }
            else
            {
                return(Json(new
                {
                    isOk = false,
                    message = "传递了错误的参数!无法登录",
                    url = "/Error/ParameterError"
                }));
            }
        }