Пример #1
0
        // [Route("salt")]
        // [HttpGet]

        // public GetLearnerInfoResponse salt(string token)
        // {
        //     try
        //     {
        //         //  var aa= Request.Headers["Content-Type"];

        //         if (string.IsNullOrEmpty(token))
        //         {

        //             return new GetLearnerInfoResponse
        //             {
        //                 StatusCode = Global.Status[responseCode.TokenError].StatusCode,
        //                 Description = Global.Status[responseCode.TokenError].Description
        //             };
        //         }
        //         var key = "2cff5601e52f4747bfb9e271fe45042a";
        //         var salt = "d31beaac47b44b45b1c6066712d49ff6";
        //         var original_value = token;
        //         var encrypted_value = CryptographyHelpers.Encrypt(key, salt, original_value);
        //         var target = CryptographyHelpers.Decrypt(key, salt, encrypted_value);


        //         return new GetLearnerInfoResponse
        //         {
        //             Name = encrypted_value,
        //             //   Identity = encrypted_value.Length.ToString(),
        //             StatusCode = encrypted_value.Length.ToString(),
        //             Description = target,

        //         };
        //     }
        //     catch (Exception ex)
        //     {

        //         return new GetLearnerInfoResponse
        //         {
        //             StatusCode = Global.Status[responseCode.ProgramError].StatusCode,
        //             Description = Global.Status[responseCode.ProgramError].Description + ex.Message
        //         };
        //     }
        // }
        private async void LogRequest(string content, string method = null, string ip = null)
        {
            try
            {
                var dbtext       = string.Empty;
                var dbmethod     = string.Empty;
                var dbip         = string.Empty;
                var contentlenth = 150;
                var shortlength  = 44;
                if (!string.IsNullOrEmpty(content))
                {
                    var lenth = content.Length;
                    dbtext = lenth > contentlenth?content.Substring(0, contentlenth) : content;
                }
                if (!string.IsNullOrEmpty(method))
                {
                    dbmethod = method.Length > shortlength?method.Substring(0, shortlength) : method;
                }
                if (!string.IsNullOrEmpty(ip))
                {
                    dbip = ip.Length > shortlength?ip.Substring(0, shortlength) : ip;
                }
                var id = string.Format(", {0}-{1}-{2}", dbip, dbmethod, dbtext);
                Log.Information("LogRequest,{0},from ip={1}", "begin" + id, Request.HttpContext.Connection.RemoteIpAddress);
                await Task.Run(() =>
                {
                    using (var logdb = new studyinContext())
                    {
                        logdb.Request.Add(new Request
                        {
                            Content = dbtext,
                            Ip      = dbip,
                            Method  = dbmethod,
                            Time    = DateTime.Now
                        });
                        logdb.SaveChanges();
                    }
                });

                Log.Information("LogRequest,{0},from ip={1}", "end" + id, Request.HttpContext.Connection.RemoteIpAddress);
            }
            catch (Exception ex)
            {
                Log.Information("LogRequest error,{0},from ip={1}", ex.Message, Request.HttpContext.Connection.RemoteIpAddress);
            }
        }
Пример #2
0
        public async Task <LoginAndQueryResponse> LoginAndQuery([FromBody] LoginAndQueryRequest inputRequest)
        {
            try
            {
                var input = JsonConvert.SerializeObject(inputRequest);
                await Task.Run(() =>
                               LogRequest(input, "LoginAndQuery", Request.HttpContext.Connection.RemoteIpAddress.ToString()));

                if (inputRequest == null)
                {
                    Log.Error("LoginAndQuery,{0}", Global.Status[responseCode.studyRequestError].Description);
                    return(new LoginAndQueryResponse
                    {
                        StatusCode = Global.Status[responseCode.studyRequestError].StatusCode,
                        Description = Global.Status[responseCode.studyRequestError].Description
                    });
                }
                Log.Information("LoginAndQuery,input={0},from {1}", input, Request.HttpContext.Connection.RemoteIpAddress);
                var allstatus       = string.Empty;
                var allow           = true;
                var completed       = true;
                var signed          = true;
                var firstsigned     = true;
                var drivinglicense  = string.Empty;
                var deductedmarks   = 0;
                var identity        = inputRequest.Identity;
                var fname           = identity + ".jpg";
                var cryptographicid = CryptographyHelpers.StudyEncrypt(identity);
                var pic             = new byte[8];


                //token process
                var toke1n    = GetToken();
                var found     = false;
                var lasttoken = string.Empty;
                foreach (var a in tokens)
                {
                    if (a.Identity == identity)
                    {
                        //  lasttoken = a.Token;
                        a.Token = toke1n;
                        found   = true;
                        break;
                    }
                }
                if (!found)
                {
                    tokens.Add(new Ptoken {
                        Identity = identity, Token = toke1n
                    });
                }

                var theuser = _db1.User.FirstOrDefault(async => (async.Identity == identity || async.Identity == cryptographicid) &&
                                                       async.Inspect == "1"
                                                       );
                if (theuser == null)
                {
                    var his = _db1.History.Where(async => async.Identity == identity || async.Identity == cryptographicid)
                              .OrderBy(al => al.Finishdate).LastOrDefault();
                    if (his == null)
                    {
                        Log.Error("LoginAndQuery,{0}", Global.Status[responseCode.studyNotNecessary].Description + identity);
                        return(new LoginAndQueryResponse
                        {
                            StatusCode = Global.Status[responseCode.studyNotNecessary].StatusCode,
                            Description = Global.Status[responseCode.studyNotNecessary].Description + identity
                        });
                    }
                    allow = his.Drugrelated != "1" ? true : false;
                    if (!string.IsNullOrEmpty(his.Status) && (his.Status.Contains("H") || his.Status.Contains("M")))
                    {
                        allow = false;
                    }
                    completed   = his.Completed == "1" ? true : false;
                    signed      = his.Signed == "1" ? true : false;
                    firstsigned = his.Firstsigned == "1" ? true : false;
                    if (!string.IsNullOrEmpty(his.Licensetype))
                    {
                        drivinglicense = his.Licensetype;
                    }
                    if (his.Deductedmarks != null)
                    {
                        deductedmarks = (int)his?.Deductedmarks;
                    }
                    try
                    {
                        if (!string.IsNullOrEmpty(his.Photofile))
                        {
                            fname = his.Photofile;
                            pic   = CryptographyHelpers.StudyFileDecrypt(Path.Combine(Global.PhotoPath, fname));
                        }
                        else
                        {
                            var filename = Path.Combine(Global.PhotoPath, fname);
                            pic = System.IO.File.ReadAllBytes(filename);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("loginandquery,{0},={1}", identity, ex.Message);
                    }
                    if (allow)
                    {
                        allstatus = his.Studylog;
                    }
                    else
                    {
                        allstatus = "您不能参加网络学习,可以参加现场学习";
                    }
                }
                else
                {
                    //drugrelated judge
                    allow = theuser.Drugrelated != "1" ? true : false;
                    if (!string.IsNullOrEmpty(theuser.Status) && (theuser.Status.Contains("H") || theuser.Status.Contains("M")))
                    {
                        allow = false;
                    }
                    completed   = theuser.Completed == "1" ? true : false;
                    signed      = theuser.Signed == "1" ? true : false;
                    firstsigned = theuser.Firstsigned == "1" ? true : false;
                    if (!string.IsNullOrEmpty(theuser.Licensetype))
                    {
                        drivinglicense = theuser.Licensetype;
                    }
                    if (theuser.Deductedmarks != null)
                    {
                        deductedmarks = (int)theuser?.Deductedmarks;
                    }
                    if (allow)
                    {
                        allstatus = theuser.Studylog;
                        //need update?
                        if (!string.IsNullOrEmpty(inputRequest.Name))
                        {
                            theuser.Name = inputRequest.Name;
                        }
                        //  theuser.Licensetype = ((int)inputRequest.DrivingLicenseType).ToString();//elements?
                        if (!string.IsNullOrEmpty(inputRequest.Phone))
                        {
                            theuser.Phone = inputRequest.Phone;
                        }
                        // theuser.Wechat = inputRequest.Wechat;
                        if (theuser.Startdate == null)
                        {
                            theuser.Startdate = DateTime.Now;
                        }
                        if (!string.IsNullOrEmpty(theuser.Token))
                        {
                            lasttoken = theuser.Token;
                        }
                        _db1.SaveChanges();
                    }
                    else
                    {
                        allstatus = "您不能参加网络学习,可以参加现场学习";
                    }
                    try
                    {
                        if (!string.IsNullOrEmpty(theuser.Photofile))
                        {
                            fname = theuser.Photofile;
                            pic   = CryptographyHelpers.StudyFileDecrypt(Path.Combine(Global.PhotoPath, fname));
                        }
                        else
                        {
                            var filename = Path.Combine(Global.PhotoPath, fname);
                            pic = System.IO.File.ReadAllBytes(filename);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("loginandquery,{0},={1}", identity, ex.Message);
                    }
                }



                return(new LoginAndQueryResponse
                {
                    Token = toke1n,
                    LastToken = lasttoken,
                    StatusCode = Global.Status[responseCode.studyOk].StatusCode,
                    Description = Global.Status[responseCode.studyOk].Description,
                    AllowedToStudy = allow,
                    Completed = completed,
                    Signed = signed,
                    FirstSigned = firstsigned,
                    DrivingLicense = drivinglicense,
                    DeductedMarks = deductedmarks,
                    Photo = pic,
                    AllStatus = allstatus
                });
            }
            catch (Exception ex)
            {
                Log.Error("LoginAndQuery,{0}", ex);
                return(new LoginAndQueryResponse
                {
                    StatusCode = Global.Status[responseCode.studyProgramError].StatusCode,
                    Description = Global.Status[responseCode.studyProgramError].Description
                });
            }
        }