Beispiel #1
0
        /// <summary>
        /// 返回 Tags 表中的所有内容
        /// </summary>
        /// <returns></returns>
        public static List <Tag> GetAllTags()
        {
            PmerDbContext dbContext = new PmerDbContext();
            var           retList   = dbContext.Tags.ToList();

            return(retList);
        }
Beispiel #2
0
        //
        /// <summary>
        /// 判断用户是否已经注册
        /// </summary>
        /// <returns>已经注册返回true, 否则返回false</returns>
        public static bool IfRegister()
        {
            string dbDirectory = ConfigurationManager.AppSettings["DataBaseDirNamePath"];
            string dbFile      = ConfigurationManager.AppSettings["DataBaseFileNamePath"];

            using (var db = new PmerDbContext())
            {
                if (!Directory.Exists(dbDirectory))
                {
                    // 若不存在数据库文件夹则创建文件夹与数据表
                    Directory.CreateDirectory(dbDirectory);
                    db.Database.EnsureCreated();
                    return(false);
                }
                else if (!File.Exists(dbFile))
                {
                    // 存在文件夹不存在数据库文件
                    db.Database.EnsureCreated();
                    return(false);
                }
                else if (!IfMPContainData())
                {
                    // 存在数据库文件,表中无数据
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
Beispiel #3
0
        public static int GetTagId(string TagNameAdded)
        {
            PmerDbContext dbContext = new PmerDbContext();
            var           tagItem   = dbContext.Tags.Single(tg => tg.TagName.Equals(TagNameAdded));

            return(tagItem.TagId);
        }
Beispiel #4
0
        public static void UpdatePasswordItem(PasswordItem passwordItem)
        {
            PmerDbContext dbContext = new PmerDbContext();

            dbContext.PasswordItems.Update(passwordItem);
            dbContext.SaveChanges();
        }
Beispiel #5
0
        /// <summary>
        /// testing
        /// </summary>
        /// <param name="tag"></param>
        public static void InsertTag(Tag tag)
        {
            PmerDbContext dbContext = new PmerDbContext();

            dbContext.Tags.Add(tag);
            dbContext.SaveChanges();
        }
Beispiel #6
0
        /// <summary>
        /// 将添加的密码项插入表中
        /// </summary>
        /// <param name="passwordItem"></param>
        public static void InsertPasswordItem(PasswordItem passwordItem)
        {
            PmerDbContext dbContext = new PmerDbContext();

            // 修改过
            dbContext.PasswordItems.Add(passwordItem);
            dbContext.SaveChanges();
        }
Beispiel #7
0
        // 一些工具函数
        /// <summary>
        /// MP: MainPassword
        /// </summary>
        /// <returns></returns>
        private static bool IfMPContainData()
        {
            // 检查MainPassword中是否含有数据
            PmerDbContext dbContext = new PmerDbContext();
            int           item_nums = dbContext.MainPassword.ToList().Count();

            return(item_nums > 0 ? true : false);
        }
Beispiel #8
0
        /// <summary>
        /// 删除置顶 Id 的密码项
        /// </summary>
        /// <param name="id">密码项目的 Id</param>
        public static void DeletePasswordItem(int id)
        {
            PmerDbContext dbContext    = new PmerDbContext();
            PasswordItem  passwordItem = dbContext.PasswordItems.Find(id);

            dbContext.PasswordItems.Remove(passwordItem);
            dbContext.SaveChanges();
        }
Beispiel #9
0
        /// <summary>
        /// 从库中取回密码项,并对密码解密
        /// </summary>
        /// <param name="tagId"></param>
        /// <returns>明文密码项目</returns>
        public static List <PasswordItem> GetAllPasswordItemFromTag(int tagId, byte[] keyPassword)
        {
            PmerDbContext dbContext = new PmerDbContext();
            var           passwords = dbContext.PasswordItems.Where(pw => pw.TagId == tagId).ToList();

            passwords = DecrypPasswordItem(passwords, keyPassword);
            return(passwords);
        }
Beispiel #10
0
        /// <summary>
        /// 获取所有的密码项
        /// </summary>
        /// <param name="keyPassword">用于解密的密钥</param>
        /// <returns></returns>
        public static List <PasswordItem> GetAllPasswordItems(byte[] keyPassword)
        {
            PmerDbContext       dbContext     = new PmerDbContext();
            List <PasswordItem> passwordItems = dbContext.PasswordItems.ToList();

            passwordItems = DecrypPasswordItem(passwordItems, keyPassword);
            return(passwordItems);
        }
Beispiel #11
0
        /// <summary>
        /// 根据密码名查询密码
        /// </summary>
        /// <param name="passwordName"></param>
        /// <returns>查询成功返回该密码项,失败返回 null</returns>
        public static PasswordItem SearchPasswordByName(string passwordName)
        {
            PmerDbContext dbContext = new PmerDbContext();
            // 忽略大小写比较,本应该使用 string.Equals(str1,str2,System.StringComparison.OrdinalIgnoreCase)
            // 但 Linq 中不支持 StringComparison.OrdinalIgnoreCase
            PasswordItem retPassword = dbContext.PasswordItems.SingleOrDefault(pw => pw.Title.ToLower().Equals(passwordName.ToLower()));

            return(retPassword);
        }
Beispiel #12
0
        public static MainPassword GetMainPasswordItem()
        {
            PmerDbContext dbContext = new PmerDbContext();

            // 此处因为主密码表中只有一个id,且一定值为1,故如此查询
            // 后期维护改进
            MainPassword mainPassword = dbContext.MainPassword.Single(p => p.Id == 1);

            return(mainPassword);
        }
Beispiel #13
0
        /// <summary>
        /// 插入 tag name 返回 tag id
        /// </summary>
        /// <param name="tagName"></param>
        /// <returns>tag id</returns>
        public static int InsertTagName(string tagName)
        {
            PmerDbContext dbContext = new PmerDbContext();
            Tag           tg        = new Tag
            {
                TagName = tagName
            };

            dbContext.Tags.Add(tg);
            dbContext.SaveChanges();

            var lastTag = dbContext.Tags.OrderBy(tg => tg.TagId).Last();

            return(lastTag.TagId);
        }
Beispiel #14
0
        /// <summary>
        /// 获取 Tags 标中最大的 TagId
        /// </summary>
        /// <returns></returns>
        public static int GetMaxTagId()
        {
            PmerDbContext dbContext = new PmerDbContext();
            int           retId     = 0;

            if (dbContext.Tags.ToList().Count() != 0)
            {
                retId = dbContext.Tags.Select(t => t.TagId).Max();
            }
            else
            {
                retId = -1;
            }
            return(retId);
        }
Beispiel #15
0
        /// <summary>
        /// 将主密码以及用户名和盐插入MainPassword表中
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="preSalt"></param>
        /// <param name="sufSalt"></param>
        /// 改进:添加错误处理
        public static void InsertMainPassword(string username, string password, string preSalt, string sufSalt)
        {
            MainPassword mainPassword = new MainPassword
            {
                Username = username,
                Password = password,
                PreSalt  = preSalt,
                SufSalt  = sufSalt
            };
            PmerDbContext dbContext = new PmerDbContext();

            // dbContext.Add(mainPassword); // 修改过
            dbContext.MainPassword.Add(mainPassword);

            dbContext.SaveChanges();
        }