public void Handle(SuccessfulLoginEvent <CustomUserAccount> evt) { using (var db = new CustomDatabase()) { var audit = new AuthenticationAudit { Date = DateTime.UtcNow, Activity = "Login Success", Detail = null, ClientIP = HttpContext.Current.Request.UserHostAddress, }; db.Audits.Add(audit); db.SaveChanges(); } }
public void UploadImage(string imagePath, string caption, bool crop = false, bool withBorder = false) { if (crop) { imagePath = Crop(imagePath, withBorder); } try { string guid; string deviceId; //save our device/guid so we look like we're always uploading from the same phone if (!string.IsNullOrEmpty(Properties.Settings.Default.deviceId)) { guid = Properties.Settings.Default.guid; deviceId = Properties.Settings.Default.deviceId; } else { guid = GenerateGuid(); deviceId = $"android-{guid}"; Properties.Settings.Default.deviceId = deviceId; Properties.Settings.Default.guid = guid; Properties.Settings.Default.Save(); } var data = new Dictionary <string, string> { { "device_id", deviceId }, { "guid", guid }, { "username", _username }, { "password", StringUtilities.SecureStringToString(_password) }, { "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" } }; var loginData = serializer.Serialize(data); var signature = GenerateSignature(loginData); var signedLoginData = $"signed_body={signature}.{HttpUtility.UrlEncode(loginData)}&ig_sig_key_version=6"; OnLoginEvent?.Invoke(this, new NormalResponse { Status = "ok", Message = "Logging in please wait." }); var loginResponse = instagramApi.PostData("accounts/login/", signedLoginData, _userAgent); if (string.IsNullOrEmpty(loginResponse)) { ErrorEvent?.Invoke(this, new ErrorResponse { Status = "fail", Message = "Empty response received from the server while trying to login" }); } else { try { var loginJson = JObject.Parse(loginResponse); var status = (string)loginJson["status"]; if (status.Equals("ok")) { var username = (string)loginJson["logged_in_user"]["username"]; var hasAnonymousProfilePicture = (bool)loginJson["logged_in_user"]["has_anonymous_profile_picture"]; var profilePicUrl = (string)loginJson["logged_in_user"]["profile_pic_url"]; var fullName = (string)loginJson["logged_in_user"]["full_name"]; var isPrivate = (bool)loginJson["logged_in_user"]["is_private"]; SuccessfulLoginEvent?.Invoke(this, new LoggedInUserResponse { Username = username, HasAnonymousProfilePicture = hasAnonymousProfilePicture, ProfilePicUrl = profilePicUrl, FullName = fullName, IsPrivate = isPrivate }); OnMediaUploadStartedEvent?.Invoke(this, EventArgs.Empty); var uploadResponse = instagramApi.PostImage(imagePath, _userAgent); if (string.IsNullOrEmpty(uploadResponse)) { ErrorEvent?.Invoke(this, new ErrorResponse { Status = "fail", Message = "Empty response received from the server while trying to post the image" }); } else { try { var uploadJson = JObject.Parse(uploadResponse); var uploadStatus = (string)uploadJson["status"]; if (uploadStatus.Equals("ok")) { OnMediaUploadeComplete?.Invoke(this, EventArgs.Empty); OnMediaConfigureStarted?.Invoke(this, EventArgs.Empty); var newLineStripper = new Regex(@"/\r|\n/", RegexOptions.IgnoreCase); //... caption = newLineStripper.Replace(caption, ""); var mediaId = (string)uploadJson["media_id"]; var configureData = new Dictionary <string, string> { { "device_id", deviceId }, { "guid", guid }, { "media_id", mediaId }, { "caption", caption.Trim() }, { "device_timestamp", StringUtilities.GenerateTimeStamp() }, { "source_type", "5" }, { "filter_type", "0" }, { "extra", "{}" }, { "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" } }; var configureDataString = serializer.Serialize(configureData); var configureSignature = GenerateSignature(configureDataString); var signedConfigureBody = $"signed_body={configureSignature}.{HttpUtility.UrlEncode(configureDataString)}&ig_sig_key_version=4"; var configureResults = instagramApi.PostData("media/configure/", signedConfigureBody, _userAgent); if (string.IsNullOrEmpty(configureResults)) { ErrorEvent?.Invoke(this, new ErrorResponse { Status = "fail", Message = "Empty response received from the server while trying to configure the image" }); } else { try { var configureJson = JObject.Parse(configureResults); var configureStatus = (string)configureJson["status"]; if (configureStatus.Equals("fail")) { ErrorEvent?.Invoke(this, new ErrorResponse { Status = "fail", Message = (string)configureJson["message"] }); } else if (configureStatus.Equals("ok")) { var uploadedResponse = new UploadResponse { Images = new List <UploadResponse.InstagramMedia>() }; foreach ( var media in configureJson["media"]["image_versions2"]["candidates"].Select( x => JObject.Parse(x.ToString())) .Select(mediaJson => new UploadResponse.InstagramMedia { Url = (string)mediaJson["url"], Width = (int)mediaJson["width"], Height = (int)mediaJson["height"] })) { uploadedResponse.Images.Add(media); } OnCompleteEvent?.Invoke(this, uploadedResponse); } } catch (Exception ex) { ErrorEvent?.Invoke(this, new ErrorResponse { Status = "fail", Message = "Could not decode the configure response" }); } } } else { ErrorEvent?.Invoke(this, new ErrorResponse { Status = "fail", Message = (string)uploadJson["message"] }); } } catch (Exception) { ErrorEvent?.Invoke(this, new ErrorResponse { Status = "fail", Message = "Could not decode the upload response" }); } } } else { var message = (string)loginJson["message"]; InvalidLoginEvent?.Invoke(this, new ErrorResponse { Status = status, Message = message }); } } catch (Exception) { ErrorEvent?.Invoke(this, new ErrorResponse { Status = "fail", Message = "Could not decode the login response" }); } } } finally { //clean up if (crop) { if (File.Exists(imagePath)) { File.Delete(imagePath); } } } }
public void SuccessfulLogin(bool isSuccessful) { SuccessfulLoginEvent?.Invoke(isSuccessful); }
public void Handle(SuccessfulLoginEvent <CustomUserAccount> evt) { //can do custom processing here... var stop = false; }