protected override void SaveNew() { using (DbModelContainer db = new DbModelContainer()) { User user = new User() { Firstname = this.Firstname, Surname = this.Surname, Login = this.Login, IsSysAdmin = this.SysAdmin, Salt = "salt", Hash = "hash" }; db.Users.Add(user); db.SaveChanges(); user.ChangePassword(TempPassword); user.NewPasswordDue = true; db.SaveChanges(); //access rights foreach (Index index in AvailableIndices.Where(i => i.IsSelected)) { Index chosenIndex = db.Indices.Find(index.Id); user.Indices.Add(chosenIndex); } db.SaveChanges(); } }
public Person AddPerson(Person newPerson) { var addedPerson = _ctx.Personal.Add(newPerson); _ctx.SaveChanges(); return(addedPerson); }
/// <summary> /// Creates the initial user record necessary to log in as /// sys admin and create subsequent records. Only here as /// a convenience to be called with VS immediate window. /// </summary> /// <param name="username">The username for the new user</param> /// <param name="password">Password for the new user</param> /// <param name="firstname">First name of the new user</param> /// <param name="lastname">Last name of the new user</param> private static void CreateInitialUserRecord (string username, string password, string firstname, string lastname) { using (DbModelContainer db = new DbModelContainer()) { User user = new User() { Firstname = firstname, Surname = lastname, Login = username, IsSysAdmin = true, Salt = "salt", Hash = "hash", NewPasswordDue = true }; db.Users.Add(user); db.SaveChanges(); user.ChangePassword(password); user.NewPasswordDue = true; db.SaveChanges(); } }
public IHttpActionResult Add(Menu menu) { menu.Id = Guid.NewGuid().ToString(); DbContext.Menu.Add(menu); try { DbContext.SaveChanges(); return(Json(new { result_code = "success", msg = "添加成功", data = menu }, JsonConfig.jsSettings)); } catch (Exception ex) { return(Json(new { result_code = "error", msg = ex.Message, data = menu }, JsonConfig.jsSettings)); } }
public static void Init() { using (DbModelContainer DbContext = new DbModelContainer()) { //初始化管理员用户 var adminUser = new User(); var users = from m in DbContext.User where m.Name == "admin" select m; if (users.Count() == 0) { User user = new User() { Id = Guid.NewGuid().ToString(), Name = "admin", Password = Encrypt.Md5("123456"), CreateTime = DateTime.Now, Remarks = "系统生成超级管理员" }; DbContext.User.Add(user); adminUser = user; } else { adminUser = users.First(); } var ids = from m in DbContext.UserProperty where m.UserId == adminUser.Id select m.MenuId; var newMenus = (from m in DbContext.Menu where !ids.Contains(m.Id) select m).ToList().Select(m => new UserProperty { Id = Guid.NewGuid().ToString(), MenuId = m.Id, UserId = adminUser.Id, CreateTime = DateTime.Now, CreateUser = "******" }); DbContext.UserProperty.AddRange(newMenus); DbContext.SaveChanges(); } }
public void ResetCode() { using (var DbModelContainer = new DbModelContainer()) { DbModelContainer.SalleEntityJeu.Find(this.ID).Code = null; DbModelContainer.SaveChanges(); } }
public bool Add(Party entity) { try { if (entity.Name == null) { throw new Exception("Name field must not be null"); } _dbContext.PartySet.Add(entity); _dbContext.SaveChanges(); return(true); } catch (Exception ex) { _logger.LogError(ex.StackTrace); return(false); } }
public void Update(int val) { try { if (_dbContext.Statistics.Count() == 0) { _dbContext.Statistics.Add(new Statistics() { BlockedAttempts = 0 }); _dbContext.SaveChanges(); } _dbContext.Statistics.First().BlockedAttempts = val; _dbContext.SaveChanges(); } catch (System.Exception ex) { _logger.LogError(ex.StackTrace); } }
public bool Add(Voter entity) { try { if (entity.FirstName == null || entity.LastName == null || entity.Pesel == null) { throw new Exception("Name and Pesel fields must not be null"); } entity.Pesel = _encryption.Hash(entity.Pesel); _dbContext.VoterSet.Add(entity); _dbContext.SaveChanges(); return(true); } catch (Exception ex) { _logger.LogError(ex.StackTrace); return(false); } }
public IHttpActionResult Login(UserView user) { if (string.IsNullOrEmpty(user.Name)) { return(Json(new { result_code = "fail", msg = "用户名不能为空" })); } if (string.IsNullOrEmpty(user.Password)) { return(Json(new { result_code = "fail", msg = "密码不能为空" })); } if (user.remember != "1") { user.Password = Encrypt.Md5(user.Password); } var users = from m in DbContext.User where m.Name == user.Name && m.Password == user.Password select m; if (users.Count() > 0) { var u = users.First(); //记录日志 UserLog log = new UserLog() { Id = Guid.NewGuid().ToString(), Type = "LOGIN", Content = string.Format("主机名:{0},IP:{1},浏览器:{2},操作系统:{3}", RequestHelper.GetUserHostName(), RequestHelper.GetIP(), RequestHelper.GetBrowser(), RequestHelper.GetOSVersion()), UserId = u.Id, UserName = u.Name, CreateTime = DateTime.Now }; DbContext.UserLog.Add(log); DbContext.SaveChanges(); //保存登录用户信息 UserHelper.SetLoginUser(u); return(Json(new { result_code = "success", msg = "登录成功", data = u }, JsonConfig.jsSettings)); } else { return(Json(new { result_code = "fail", msg = "用户名或密码错误" })); } }
private static void LogFileChange(TrackedFile file, long newLength, DateTime newModifiedDate) { using (DbModelContainer db = new DbModelContainer()) { db.Database.CommandTimeout = 1800; db.TrackedFiles.Attach(file); file.Length = newLength; file.LastSeen = newModifiedDate; if (file.UpdatesSeen == null || file.UpdatesSeen == 0) { file.UpdatesSeen = 1; } else { file.UpdatesSeen = file.UpdatesSeen + 1; } db.SaveChanges(); } }
static void Main(string[] args) { int totalProcessed = 0; int totalHashed = 0; int totalFailed = 0; IEnumerable <FilesDueForHash_Result> filesToHash = GetNextFiles(); do { Parallel.ForEach(filesToHash, fileToHash => { Console.WriteLine("Processing: " + fileToHash.FullPath); try { if (File.Exists(fileToHash.FullPath) && fileToHash.Length > 0) { Stream fileStream = File.OpenRead(fileToHash.FullPath); IHashMaker hasher = new HashMaker(); string preHash = hasher.GetPreHash(fileToHash.FullPath); string md5 = hasher.GetMD5(fileStream); string sha1 = hasher.GetSHA1(fileStream); using (DbModelContainer db = new DbModelContainer()) { db.Database.CommandTimeout = 60; TrackedFile fileToUpdate = db.TrackedFiles.Find(fileToHash.Id); fileToUpdate.PreHash = preHash; fileToUpdate.MD5 = md5; fileToUpdate.SHA1 = sha1; db.SaveChanges(); } Interlocked.Increment(ref totalHashed); } } catch (Exception) { //mark it as failed Interlocked.Increment(ref totalFailed); } finally { Interlocked.Increment(ref totalProcessed); using (DbModelContainer db = new DbModelContainer()) { db.Database.CommandTimeout = 60; TrackedFile file = db.TrackedFiles.Find(fileToHash.Id); file.HashAttempted = DateTime.Now; db.SaveChanges(); } } }); filesToHash = GetNextFiles(); } while (filesToHash.Count() > 0); }