Ejemplo n.º 1
0
        public ActionResult <List <Course> > getCoursTable()
        {
            Console.WriteLine("getting CourseTable");
            JwCrawler.initAttributes();

            // 得到当前的http请求
            var    req           = HttpContext.Request;
            string tokenToDecode = req.Headers["Authorization"].ToString().Split(' ').Last();     // 获取token
            // 解码的token转化为json对象
            JObject token    = (JObject)JsonConvert.DeserializeObject(DecodeJwt.decode(tokenToDecode));
            string  token_id = token["sub"].ToString();     // 拿到id

            Console.WriteLine($"id frome token: {token_id}");

            // 根据id从数据库里面查询密码
            User query = db.users.FirstOrDefault(
                o => o.id == Convert.ToInt64(token_id)
                );

            // 调用爬虫
            JwCrawler.initAttributes();
            JwCrawler.login(query.id, getPwdEncrypted(query.portalpwd, JwCrawler.dynamicPwdEncryptSalt).Result);
            JwCrawler.loginJw();    // 登录教务
            List <Course> courses = CourseTableCrawler.crawlCourseTable();

            JwCrawler.logout();
            return(courses);
        }
Ejemplo n.º 2
0
        public ActionResult suspendNetwork()
        {
            JwCrawler.initAttributes();

            // 得到当前的http请求
            var    req           = HttpContext.Request;
            string tokenToDecode = req.Headers["Authorization"].ToString().Split(' ').Last();     // 获取token
            // 解码的token转化为json对象
            JObject token    = (JObject)JsonConvert.DeserializeObject(DecodeJwt.decode(tokenToDecode));
            string  token_id = token["sub"].ToString();     // 拿到id

            Console.WriteLine($"id frome token: {token_id}");

            // 根据id从数据库里面查询密码
            User query = db.users.FirstOrDefault(
                o => o.id == Convert.ToInt64(token_id)
                );

            // 调用爬虫
            JwCrawler.initAttributes();
            JwCrawler.login(query.id, getPwdEncrypted(query.portalpwd, JwCrawler.dynamicPwdEncryptSalt).Result);

            NetworkCrawler.CrawlNetorkStatus(query.id);
            NetworkCrawler.suspendNetwork(query.id);
            JwCrawler.logout();
            return(Ok());
        }
        public ActionResult <List <CourseScore> > getCourseScore()
        {
            Console.WriteLine("getting CourseScore");
            JwCrawler.initAttributes();

            // 得到当前的http请求
            var    req           = HttpContext.Request;
            string tokenToDecode = req.Headers["Authorization"].ToString().Split(' ').Last();     // 获取token
            // 解码的token转化为json对象
            JObject token    = (JObject)JsonConvert.DeserializeObject(DecodeJwt.decode(tokenToDecode));
            string  token_id = token["sub"].ToString();     // 拿到id

            Console.WriteLine($"id frome token: {token_id}");

            // 根据id从数据库里面查询密码
            User query = db.users.FirstOrDefault(
                o => o.id == Convert.ToInt64(token_id)
                );

            // 调用爬虫
            JwCrawler.initAttributes();
            JwCrawler.login(query.id, getPwdEncrypted(query.portalpwd, JwCrawler.dynamicPwdEncryptSalt).Result);
            JwCrawler.loginJw();
            List <CourseScore> score = CourseScoreCrawler.crawlCourseScore(Convert.ToInt64(token_id));

            JwCrawler.logout();
            // 成绩顺便保存到数据库中
            foreach (CourseScore s in score)
            {
                CourseScore sQuery = db.courseScore.Where(
                    o =>
                    o.courseid == s.courseid &&
                    o.userid == s.userid
                    ).FirstOrDefault();

                if (sQuery == null && s.score != -1)
                {
                    db.courseScore.Add(s);
                }
            }

            db.SaveChanges();
            return(score);
        }
Ejemplo n.º 4
0
        public IActionResult login([FromBody] User user)
        {
            User query;

            // 首先尝试从数据库读取用户信息
            try {
                query = db.users.FirstOrDefault(
                    o => o.id == user.id
                    );
            }
            catch (Exception e) {
                return(BadRequest(e.InnerException.Message));
            }
            // 查询不到用户或者密码验证不通过应尝试登录信息门户 若无法登录返回错误
            if (query == null || query.password != user.password || query.portalpwd != user.portalpwd)

            {
                JwCrawler.initAttributes();
                bool result = JwCrawler.login(user.id, getPwdEncrypted(user.portalpwd, JwCrawler.dynamicPwdEncryptSalt).Result);

                if (!result)
                {
                    return(BadRequest("wrong password!"));
                }

                // 信息门户登录成功的话,爬下来个人信息保存到数据库
                JwCrawler.loginJw();                                  // 登录教务
                User newUser = StudentInfoCrawler.crawlStudentInfo(); // 爬取个人信息
                newUser.password  = user.password;
                newUser.portalpwd = user.portalpwd;
                if (query == null)
                {
                    db.users.Add(newUser);
                }
                else
                {
                    db.users.Update(newUser);
                }

                db.SaveChanges();

                // 退出登录
                JwCrawler.logout();
            }


            var authClaims = new [] {
                // subject 身份
                new Claim(JwtRegisteredClaimNames.Sub, user.id.ToString()),
                // jwt id 由Guid.NewGuid()生成一个唯一标识符
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
            };

            // 通过启用 IdentityModelEventSource.ShowPII = true 来查看完整的异常信息.
            IdentityModelEventSource.ShowPII = true;
            // 密钥
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("*****@*****.**"));
            //生成token 需要nuget添加Microsoft.AspNetCore.Authentication.JwtBearer包,并引用System.IdentityModel.Tokens.Jwt命名空间
            var token = new JwtSecurityToken(
                // issuer代表颁发token的程序
                issuer: "LuoJiaCampus_Server",
                // audience代表受理token的程序
                audience: "LuoJiaCampush_Client",
                // 过期时间
                expires: DateTime.Now.AddHours(2),
                claims: authClaims,
                signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
                );


            return(Ok(
                       new {
                // 返回token
                token = new JwtSecurityTokenHandler().WriteToken(token),
                // 返回过期时间
                expiration = token.ValidTo
            }
                       ));
        }