public RegError Register(UserInfo userInfo) { using (TransactionScope transaction = new TransactionScope()) { using (ShareWareEntities context = new ShareWareEntities()) { try { var result = from c in context.Users where c.UserName == userInfo.UserName select c; if (result.Count() > 0) { return(RegError.UserExist); } Users newUser = new Users() { UserName = userInfo.UserName, Password = userInfo.Password, NickName = userInfo.NickName, IsMale = userInfo.IsMale, QQ = userInfo.QQ, MicroBlog = userInfo.MicroBlog, Signature = userInfo.Signature }; context.Users.Add(newUser); context.SaveChanges(); var user = context.Users.Single(T => T.UserName == newUser.UserName); string nameHash = ShareService.ComputeStringMd5(user.UserName); string imagePath = ConfigurationManager.AppSettings["ImagePath"]; if (!Directory.Exists(imagePath)) { Directory.CreateDirectory(imagePath); } if (userInfo.Image != null) { string filePath = imagePath + nameHash.ToString() + ".jpg"; userInfo.Image.Save(filePath, ImageFormat.Jpeg); string fileHash = ShareService.ComputeFileMd5(filePath); user.ImageHash = fileHash; context.SaveChanges(); } transaction.Complete(); } catch (Exception) { return(RegError.Ohter); } } } return(RegError.NoError); }
public bool ChangePassword(string oldPassword, string newPassword) { int id = GetClientId(); if (id < 0) { return(false); } using (ShareWareEntities context = new ShareWareEntities()) { try { Users user = context.Users.Single(T => T.UserID == id); if (user.Password == oldPassword) { user.Password = newPassword; context.SaveChanges(); } else { return(false); } } catch (Exception) { return(false); } } return(true); }
public bool ChangeUserInfo(UserInfo userInfo) { int id = GetClientId(); if (id < 0) { return(false); } using (ShareWareEntities context = new ShareWareEntities()) { try { Users user = context.Users.Single(T => T.UserID == id); user.NickName = userInfo.NickName; user.IsMale = userInfo.IsMale; user.QQ = userInfo.QQ; user.MicroBlog = userInfo.MicroBlog; user.Signature = userInfo.Signature; context.SaveChanges(); } catch (Exception) { return(false); } } return(true); }
public void UploadImage(Bitmap image) { int id = GetClientId(); if (id < 0) { return; } using (ShareWareEntities context = new ShareWareEntities()) { var user = context.Users.Single(T => T.UserID == id); string nameHash = ComputeStringMd5(user.UserName); string imagePath = ImagePath; if (!Directory.Exists(imagePath)) { Directory.CreateDirectory(imagePath); } string filePath = imagePath + nameHash.ToString() + ".jpg"; image.Save(filePath, ImageFormat.Jpeg); string fileHash = ComputeFileMd5(filePath); user.ImageHash = fileHash; context.SaveChanges(); } }
public void RemoveOldFile(List <FileInfoTransfer> fileList) { int id = GetClientId(); if (id < 0) { return; } using (ShareWareEntities context = new ShareWareEntities()) { foreach (var item in fileList) { var res = from c in context.FileOwner where c.UserID == id && c.Hash == item.Hash select c; foreach (var file in res) { context.FileOwner.Remove(file); } try { context.SaveChanges(); } catch (Exception) { // throw; } } } }
public void RemoveNotExistShreFileList(List <string> hashList) { int id = GetClientId(); if (id < 0) { return; } try { using (ShareWareEntities context = new ShareWareEntities()) { foreach (var hash in hashList) { var file = from c in context.FileOwner where (c.UserID == id && c.Hash == hash) select c; foreach (var item in file) { context.FileOwner.Remove(item); } } context.SaveChanges(); } } catch (Exception) { return; } }
public int Login(string userName, string passWord, string mac) { Users user = null; try { user = _context.Users.Single(us => us.UserName == userName && us.Password == passWord); } catch (InvalidOperationException) { return(-1); } catch (Exception) { return(-1); } user.UserIP = GetClientIp(); user.MAC = mac; var client = OperationContext.Current.GetCallbackChannel <IClient>(); ClientCallbackList.Add(client); try { lock (o) { _userDict.Add(user, client); } } catch (Exception) { if (_userDict.ContainsKey(user)) { Channel_Closing(_userDict[user], new ServerEventArgs() { Message = "你已经在其他位置登陆", User = user }); _userDict[user] = client; } } try { _context.SaveChanges(); } catch (Exception) { //throw; } //BroadcastEvent(this, new ServerEventArgs() { User = user }, UserLogin); if (UserLogin != null) { UserLogin(this, new ServerEventArgs() { User = user }); } var userList = (from c in _userDict.Keys where (c.UserID != user.UserID) select new OnlineUserInfo() { UserName = c.UserName, ImageHash = c.ImageHash, NickName = c.NickName }).ToList(); client.RefreshUserList(userList); return(user.UserID); }