Esempio n. 1
0
        public async Task TryLoginAsync(UserNotFoundReceipt e)
        {
            using (APPDbContext db = new APPDbContext())
            {
                bool b_UserVertifyEvent        = false;
                bool b_UserPwdVertifyFailEvent = false;
                bool b_UserNotFoundEvent       = false;

                await Task.Run(() =>
                {
                    string pwd_hash_aes = AssetsController.EncryptwithAppaesobj(PWD_hash);

                    var iuserlist = from t in db.Users.ToList()
                                    where t.pid == PID
                                    select t;
                    var userlist = iuserlist.ToList();
                    if (userlist.Count == 0)
                    {
                        switch (e)
                        {
                        case UserNotFoundReceipt.Create:
                            User dbuser = new User()
                            {
                                pid = PID,
                                pwd = pwd_hash_aes
                            };
                            db.Entry(dbuser).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                            db.SaveChanges();
                            break;

                        case UserNotFoundReceipt.None:
                            b_UserNotFoundEvent = true;
                            return;
                        }
                    }
                    else
                    {
                        var vertifyuser = userlist[0];
                        if (vertifyuser.pid != this.PID || vertifyuser.pwd != pwd_hash_aes)
                        {
                            b_UserPwdVertifyFailEvent = true;
                        }
                    }
                    int uid = (from t in db.Users.ToList()
                               where PID == t.pid && pwd_hash_aes == t.pwd
                               select t).ToList().ElementAt(0).ID;

                    var rlist = (from t in db.Recordings.ToList()
                                 where t.uid == uid
                                 select t).ToList();

                    Core.Current.Regist(new CurrentUser(rlist, PID, PWD_hash, uid));
                    b_UserVertifyEvent = true;
                });



                if (b_UserPwdVertifyFailEvent)
                {
                    UserPwdVertifyFailEvent?.Invoke(this);
                }
                if (b_UserVertifyEvent)
                {
                    UserVertifyEvent?.Invoke(this);
                }
                if (b_UserNotFoundEvent)
                {
                    UserNotFoundEvent?.Invoke(this);
                }
            }
        }
Esempio n. 2
0
        //useage
        //var picker = new FileOpenPicker();
        //picker.FileTypeFilter.Add(".xml");
        //    var sf = await picker.PickSingleFileAsync();
        //await DataPacManager.DeserializeAsync(sf, "test");@throw KeyVertifyFailException
        public static async Task DeserializeAsync(StorageFile sf, string serkey)
        {
            var dat = await DataPac.DeserializeAsync(sf);

            string pid        = dat.pid;
            string token      = dat.token;
            string desertoken = "";

            Helper.AESProvider _AESobj = null;
            await Task.Run(() =>
            {
                //create ser aesobj
                string str1        = pid + serkey;
                string str2        = serkey + pid;
                var hashobj        = new Helper.HashProvider();
                string hstr1       = hashobj.Hash(str1);
                string hstr2       = hashobj.Hash(str2);
                string serpwd_hash = hstr1 + hstr2;
                var ivhash         = new Helper.HashProvider(HashAlgorithmNames.Md5);
                byte[] _iv         = ivhash.Hashbytes(pid);

                string kstr1 = token + serpwd_hash;
                string kstr2 = serpwd_hash + token;

                var keyhash = new Helper.HashProvider();
                byte[] _key = new byte[128];
                byte[] btar = keyhash.Hashbytes(kstr1);
                Array.Copy(btar, 0, _key, 0, 64);
                btar = keyhash.Hashbytes(kstr2);
                Array.Copy(btar, 0, _key, 64, 64);
                _AESobj    = new Helper.AESProvider(_iv, _key);
                desertoken = _AESobj.Decrypt(dat.sertoken);
            });

            if (desertoken != token)
            {
                throw new KeyVertifyFailException()
                      {
                          _DataPac = dat
                      }
            }
            ;
            await Task.Run(() =>            //InvalidOperationException
            {
                using (APPDbContext db = new APPDbContext())
                {
                    //search user
                    User user = null;
                    foreach (var t in db.Users.ToList())
                    {
                        if (t.pid == pid)
                        {
                            user = t;
                        }
                    }

                    if (user == null)
                    {
                        string pwd_hash_aes = AssetsController.EncryptwithAppaesobj(_AESobj.Decrypt(dat.pwd_hash));
                        User dbuser         = new User()
                        {
                            pid = pid,
                            pwd = pwd_hash_aes
                        };
                        db.Entry(dbuser).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                        db.SaveChanges();
                        user = db.Users.Single(b => b.pid == pid);;
                    }
                    //set user sa
                    AssetsController.getLocalSequenceString(user.ID);
                    var saobj = db.SA.Single(t => t.ID == user.ID);
                    //saobj.Data = dat.vertifystr;
                    saobj.Data            = _AESobj.Decrypt(dat.vertifystr);
                    db.Entry(saobj).State = Microsoft.EntityFrameworkCore.EntityState.Modified;

                    foreach (var t in dat.dRows)
                    {
                        Recording obj = new Recording()
                        {
                            uid   = user.ID,
                            key   = _AESobj.Decrypt(t.key),
                            value = _AESobj.Decrypt(t.value)
                        };
                        db.Entry(obj).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                    }
                    db.SaveChanges();
                }
            });
        }