/// <summary> /// Загружаем сессию пользователя из файла /// </summary> /// <returns></returns> public bool LoadSession() { string path = @application.temp_folder + "/" + Filename; if (!File.Exists(path)) { log.Error("Ошибка загрузки сессии. Файл " + Filename + " не найден"); return(false); } IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); try { UserAccessKey = (UserAccessKey)formatter.Deserialize(stream); stream.Close(); log.Info("Сессия пользователя ID:" + UserAccessKey.accessToken.user_id + " успешно загруженна"); return(true); } catch (Exception e) { log.Error("Ошибка загрузка сессии из файла " + Filename, e); stream.Close(); return(false); } }
public string GetAccessKeyByFacebook(string facebookUserId, string accessToken, bool forAdmin = false) { User user = this.GetUserModelByFacebook(facebookUserId, accessToken); if (user == null) { return(null); } else { if (forAdmin == true ? user.UserType == UserType.Admin : true) { UserAccessKey accessKey = new UserAccessKey(); accessKey.AccessKey = Encryption.GenerateID(); accessKey.UserID = user.UserID; //DB'ye ekleniyor memolineDBContext context = GenerateContext(); context.UserAccessKeys.Add(accessKey); context.SaveChanges(); return(accessKey.AccessKey); } else { return(null); } } }
/// <summary> /// Сохраняем сессию пользователя в файл /// </summary> /// <param name="session"></param> /// <returns></returns> public bool SaveSession(UserAccessKey session) { string path = @application.temp_folder + "/" + Filename; if (!File.Exists(path)) { log.Error("Файл " + Filename + " не найден."); log.Info("Создаем файл " + path); File.Create(path).Close(); } try { log.Info("Сохраняем сессию пользователя ID: " + session.accessToken.user_id); IFormatter formatter = new BinaryFormatter(); Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(stream, session); return(true); } catch (Exception e) { log.Error("Ошибка работы с файлом \n", e); return(false); } }
public string GetAccessKey(string username, string password, bool forAdmin = false) { User user = this.GetUserModelByEmailAndPassword(username, password); if (user == null) { return(null); } else { if (forAdmin == true ? user.UserType == UserType.Admin : true) { UserAccessKey accessKey = new UserAccessKey(); accessKey.AccessKey = Encryption.GenerateID(); accessKey.UserID = user.UserID; //DB'ye ekleniyor memolineDBContext context = GenerateContext(); context.UserAccessKeys.Add(accessKey); context.SaveChanges(); return(accessKey.AccessKey); } else { return(null); } } }
/// <summary> /// Получение ключа доступа для пользователя /// </summary> /// <returns></returns> public UserAccessKey UserAccessKey() { UserAccessKey account = new UserAccessKey(); HtmlContainer container; dict eventObject; string html; using (WebClientHelper helper = new WebClientHelper()) { logg.Info("Запрос на получение ключа доступа пользователя"); html = helper.DownloadString(ConnectHelpers.GenerateAuthorize(application)); while (account.code == null) { container = HtmlParser.CollectingData(html); if (container.FDictionary["action"] == "login") { logg.Info("Авторизация пользователя"); if (container.Captcha.isIsset) { logg.Info("Ожидание ввода пользователем капчи"); container.IDictionary.Add("captcha_key", _event.OnIssetCaptcha(container.Captcha.source)["captcha"]); logg.Info("Код с картинки: " + container.IDictionary["captcha_key"]); } eventObject = _event.OnIssetLogin(); logg.Info("Ожидание ввода логина и пароля"); container.IDictionary.Add("email", eventObject["email"]); container.IDictionary.Add("pass", eventObject["pass"]); logg.Info("Логин: " + eventObject["email"]); logg.Info("Пароль: " + new string('*', eventObject["pass"].Length)); html = helper.UploadString(container.FDictionary["url"], WebClientHelper.PostStringConverter(container.IDictionary)); } if (container.FDictionary["action"] == "authcheck_code") { logg.Info("Ожидаение ввода кода доступа"); eventObject = _event.OnIssetCode(); logg.Debug("Код: " + eventObject["code"]); container.IDictionary.Add("code", eventObject["code"]); container.IDictionary.Add("remember", "1"); html = helper.UploadString( "https://m.vk.com/" + container.FDictionary["url"], WebClientHelper.PostStringConverter(container.IDictionary) ); } if (container.FDictionary["action"] == "grant_access") { logg.Info("Подтверждение правил доступа"); container.IDictionary.Clear(); html = helper.DownloadString(container.FDictionary["url"]); } if (container.FDictionary["action"] == "blank") { account.code = helper.ResponseUri.ToString().Split('#')[1].Split('=')[1]; logg.Info("Получен код пользователя"); logg.Info("Код: " + account.code); } } string jsonResponse = helper.DownloadString(ConnectHelpers.GenerateAccessRequest(application, account.code)); AccessToken accessToken = JsonConvert.DeserializeObject <AccessToken>(jsonResponse); account.accessToken = accessToken; logg.Info("Токен получен: " + accessToken.access_token); helper.CookieSave(); } return(account); }