Beispiel #1
0
        private void Recordings_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            using (APPDbContext db = new APPDbContext())
            {
                switch (e.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    foreach (var t in e.NewItems)
                    {
                        var entity = new Recording();
                        db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                        db.SaveChanges();
                        var ur = ((Info)t).Encode(this, entity.ID);
                        entity.key             = ur.key;
                        entity.value           = ur.value;
                        entity.uid             = ur.uid;
                        db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    }
                    break;

                case NotifyCollectionChangedAction.Remove:
                    foreach (var t in e.OldItems)
                    {
                        db.Entry(((Info)t).Record).State =
                            Microsoft.EntityFrameworkCore.EntityState.Deleted;
                    }
                    break;

                case NotifyCollectionChangedAction.Replace:
                    foreach (var t in e.NewItems)
                    {
                        db.Entry(((Info)t).Modify(this)).State =
                            Microsoft.EntityFrameworkCore.EntityState.Modified;
                    }
                    break;

                case NotifyCollectionChangedAction.Reset:
                    foreach (var t in e.OldItems)
                    {
                        db.Entry(((Info)t).Record).State =
                            Microsoft.EntityFrameworkCore.EntityState.Deleted;
                    }
                    foreach (var t in e.NewItems)
                    {
                        var entity = new Recording();
                        db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                        db.SaveChanges();
                        var ur = ((Info)t).Encode(this, entity.ID);
                        entity.key             = ur.key;
                        entity.value           = ur.value;
                        entity.uid             = ur.uid;
                        db.Entry(entity).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    }
                    break;
                }
                db.SaveChanges();
            }
        }
Beispiel #2
0
 public static string getRecordingSequenceString(int id)
 {
     using (APPDbContext db = new APPDbContext())
     {
         var res = db.SB.Find(id);
         if (res != null)
         {
             return(res.Data);
         }
         else
         {
             var ncount = db.SB.Count();
             var rans   = new RandomGenerator();
             for (int i = 0; i < id - ncount; i++)
             {
                 db.SB.Add(new StringSequenceObjB()
                 {
                     Data = rans.getRandomString(20)
                 });
             }
             db.SaveChanges();
             res = db.SB.Find(id);
             if (res == null)
             {
                 throw new Exception("sa id miss");
             }
             return(res.Data);
         }
     }
 }
Beispiel #3
0
        private static void Set_LocalSettingChanged(LocalSetting setting)
        {
            APPDbContext db = new APPDbContext();

            db.Entry(setting.dbsetting).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            db.SaveChanges();
        }
Beispiel #4
0
        private static void _settings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            APPDbContext db = new APPDbContext();

            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                foreach (var t in e.NewItems)
                {
                    db.Entry((t as LocalSetting)?.dbsetting).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                }
            }
            else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                foreach (var t in e.OldItems)
                {
                    db.Entry((t as LocalSetting)?.dbsetting).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
                }
            }
            else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
            {
                var _old     = db.LocalSettings.Find((e.OldItems[0] as LocalSetting)?.ID);
                var _listnew = e.NewItems[0] as LocalSetting;
                _old.Key             = _listnew?.Key;
                _old.Value           = _listnew?.Value;
                db.Entry(_old).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            }
            db.SaveChanges();
        }
Beispiel #5
0
 public void DeleteUser()
 {
     using (APPDbContext db = new APPDbContext())
     {
         foreach (var t in CurrentUser.Recordings)
         {
             db.Entry(t.Record).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
         }
         db.Entry(db.Users.Single(b => b.ID == CurrentUser.UID)).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
         db.SaveChanges();
     }
 }
Beispiel #6
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);
                }
            }
        }
Beispiel #7
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();
                }
            });
        }