protected async void Page_Load(object sender, EventArgs e) { //*** First Time if (!Page.IsPostBack) { //*** Initialization divDropBoxAlert.Visible = false; lnkbtnConnectDropBox.Visible = false; lnkbtnDisconnectDropBox.Visible = false; DivDropBoxFileGrid.Visible = false; pnlDownload.Visible = false; strSiteBaseURL = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host; if (HttpContext.Current.Request.Url.Port > 0) { strSiteBaseURL += ":" + HttpContext.Current.Request.Url.Port.ToString(); } ; //*** Adjust Drop Box Call Back URL for if (HttpContext.Current.Session["dropBoxReturnBackURL"] == null) { HttpContext.Current.Session["dropBoxReturnBackURL"] = strSiteBaseURL; HttpContext.Current.Session["dropBoxReturnBackURL"] += "/" + System.Configuration.ConfigurationManager.AppSettings["dropBoxAuthReturnPage"]; } //****************************************************** //*** Check dropBox Aurhentication Token if (Application["dropBoxAccessToken"] == null) { //*************************** //*** access token is empty //*************************** //*** 1. Check first for dropBox App Key & App secret if (String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["dropBoxAppKey"]) || String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["dropBoxAppSecret"])) { //*** Show Error Message lblDropBoxMsg.Text = "Please set app key and secret in this project's Web.config file and restart. " + "App key/secret can be found in the Dropbox App Console, here: " + "https://www.dropbox.com/developers/apps"; //*** Show Error divDropBoxAlert.Visible = true; return; } else //*** AppKey & secret exist { //*** Show Connect to DropBox button lnkbtnConnectDropBox.Visible = true; } } else { //*** Get DropBox Client Object Application["dropBoxClientObj"] = DropBoxConnector.getDropboxClient(Application["dropBoxAccessToken"].ToString()); if (DropBoxConnector.MsgError != "") //*** If error { lblDropBoxMsg.Text = DropBoxConnector.MsgError; //*** Show Error divDropBoxAlert.Visible = true; } else { //*** Get User Info await DropBoxConnector.getUserInfo(Application["dropBoxClientObj"]); lblDropBoxAccountName.Text = DropBoxConnector.objDropBoxUser.AccountDisplayName; DropBoxGridDataBind((List <string>)Session["FolderPath"]); } } } //****************************************************************************************************** //*** Exact Online Part //****************************************************************************************************** //*** First Time if (!Page.IsPostBack) { //*** Initialization divExactOnlineAlert.Visible = false; lnkbtnConnectExactOnline.Visible = false; lnkbtnDisconnectExactOnline.Visible = false; DivDropBoxFileGrid.Visible = false; //*** Adjust Drop Box Call Back URL for if (HttpContext.Current.Session["exactOnlineReturnBackURL"] == null) { HttpContext.Current.Session["exactOnlineReturnBackURL"] = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host; if (HttpContext.Current.Request.Url.Port > 0) { HttpContext.Current.Session["exactOnlineReturnBackURL"] += ":" + HttpContext.Current.Request.Url.Port.ToString(); } ; HttpContext.Current.Session["exactOnlineReturnBackURL"] += "/" + System.Configuration.ConfigurationManager.AppSettings["exactOnlineReturnPage"]; } //*** Check If Code returned into Connection String if (Application["ExactOnlineAccessToken"] == null && Session["ExactOnlineReturnCode"] != null) { //**** Initialize Session Folder Path List <string> Dump = new List <string> { }; Session["ExactOnlineFolderPath"] = Dump; //**** Construct Exact Online Class ExactOnlineConnector objExactOnlineConnector = new ExactOnlineConnector(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"], new Uri(HttpContext.Current.Session["exactOnlineReturnBackURL"].ToString()), Session["ExactOnlineReturnCode"].ToString()); Application["ExactOnlineAccessToken"] = objExactOnlineConnector.GetAccessToken(); if (objExactOnlineConnector.MsgError != "") { //*** If Error returned lblExactOnlineMsg.Text = objExactOnlineConnector.MsgError; //*** Show Error divExactOnlineAlert.Visible = true; return; } } //****************************************************** //*** Check ExactOnline Aurhentication Token if (Application["ExactOnlineAccessToken"] == null) { //*************************** //*** access token is empty //*************************** //*** 1. Check first for ExactOnline App Key & App secret if (String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"]) || String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"]) || String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"])) { //*** Show Error Message lblExactOnlineMsg.Text = "Please set client id, client secret and end point URL into this project's Web.config file and restart. " + "client id/secret can be found in the ExactOnline App Console, here: " + "https://start.exactonline.co.uk"; //*** Show Error divExactOnlineAlert.Visible = true; return; } else //*** AppKey & secret exist { //*** Set URL Parameters //strExactOnlineAuthParam = ""; //*** Show Connect to ExactOnline button lnkbtnConnectExactOnline.Visible = true; } } if (Session["ExactOnlineReturnCode"] != null) { //**** Construct Exact Online Class ExactOnlineConnector objExactOnlineConnector = new ExactOnlineConnector(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"], new Uri(HttpContext.Current.Session["exactOnlineReturnBackURL"].ToString()), Session["ExactOnlineReturnCode"].ToString()); if (Application["ExactOnlineAccessToken"] != null) { objExactOnlineConnector.AccessToken = Application["ExactOnlineAccessToken"].ToString(); } //*** Get User Info lblExactOnlineAccountName.Text = objExactOnlineConnector.getUserInfo().FullName; if (objExactOnlineConnector.MsgError != "") { //*** If Error returned lblExactOnlineMsg.Text = objExactOnlineConnector.MsgError; //*** Show Error divExactOnlineAlert.Visible = true; return; } else { //*** Bind Folder Grid View ExactOnlineGridDataBind(); } } } //********************************************************************************************************* }
protected async void Page_Load(object sender, EventArgs e) { //*** Initialization divDropBoxAlert.Visible = false; lnkbtnConnectDropBox.Visible = false; lnkbtnDisconnectDropBox.Visible = false; divFileGrid.Visible = false; pnlDownload.Visible = false; //*** First Time if (!Page.IsPostBack) { //*** Check If Code returned into Connection String if (Application["dropBoxAccessToken"] == null && !String.IsNullOrEmpty(Request.QueryString["Code"])) { //**** Initialize Session Folder Path List <string> Dump = new List <string> { }; Session["FolderPath"] = Dump; await DropBoxConnector.getAccessTokenFromResponse(Request.QueryString["Code"], System.Configuration.ConfigurationManager.AppSettings["dropBoxAppKey"], System.Configuration.ConfigurationManager.AppSettings["dropBoxAppSecret"], HttpContext.Current.Session["returnBackURL"].ToString()); if (DropBoxConnector.MsgError == "") { //*** Get Token Application["dropBoxAccessToken"] = DropBoxConnector.dropBoxAccessToken; } else //*** If Error returned { lblDropBoxMsg.Text = DropBoxConnector.MsgError; //*** Show Error divDropBoxAlert.Visible = true; } } //****************************************************** //*** Check dropBox Aurhentication Token if (Application["dropBoxAccessToken"] == null) { //*************************** //*** access token is empty //*************************** //*** 1. Check first for dropBox App Key & App secret if (String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["dropBoxAppKey"]) || String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["dropBoxAppSecret"])) { //*** Show Error Message lblDropBoxMsg.Text = "Please set app key and secret in this project's Web.config file and restart. " + "App key/secret can be found in the Dropbox App Console, here: " + "https://www.dropbox.com/developers/apps"; //*** Show Error divDropBoxAlert.Visible = true; return; } else //*** AppKey & secret exist { //*** Show Connect to DropBox button lnkbtnConnectDropBox.Visible = true; } } else { //*** Get DropBox Client Object Application["dropBoxClientObj"] = DropBoxConnector.getDropboxClient(Application["dropBoxAccessToken"].ToString()); if (DropBoxConnector.MsgError != "") //*** If error { lblDropBoxMsg.Text = DropBoxConnector.MsgError; //*** Show Error divDropBoxAlert.Visible = true; } else { //*** Get User Info await DropBoxConnector.getUserInfo(Application["dropBoxClientObj"]); lblAccountName.Text = DropBoxConnector.objDropBoxUser.AccountDisplayName; DropBoxGridDataBind((List <string>)Session["FolderPath"]); } } } }
public async System.Threading.Tasks.Task <ActionResult> Index() { //*** Check Lock Variable First if (!(Boolean)HttpContext.Application["SyncInProgress"]) { HttpContext.Application["SyncInProgress"] = true; //*** Set Lock Flag ViewBag.Title = "Sync Dropbox Files"; //*** Initialization Session["lblDropBoxMsg"] = ""; Session["btnDropBoxbtnV"] = "hidden"; Session["btnExactOnlinebtnV"] = "hidden"; Session["SyncAllNumbers"] = ""; Session["ResultNumbers"] = ""; strSiteBaseURL = Request.Url.Scheme + "://" + Request.Url.Host; if (Request.Url.Port > 0) { strSiteBaseURL += ":" + Request.Url.Port.ToString(); } ; //*** Adjust Drop Box Call Back URL for if (Session["dropBoxReturnBackURL"] == null) { Session["dropBoxReturnBackURL"] = strSiteBaseURL; Session["dropBoxReturnBackURL"] += "/" + System.Configuration.ConfigurationManager.AppSettings["dropBoxAuthReturnPage"]; } //****************************************************** //********************************************* //*** Query on Files last Modified //********************************************* DateTime dtLastModifiedDate = new DateTime(2000, 1, 1); CloudStorageEntities objCloudStorageEntities = new CloudStorageEntities(); //*** Check First if File Already exisit into DB DropboxWebhook objRecord = objCloudStorageEntities.DropboxWebhooks.Where(i => i.DW_Processed == 0).FirstOrDefault(); if (objRecord != null) { dtLastModifiedDate = Convert.ToDateTime(objRecord.DW_TimeStamp); } if (dtLastModifiedDate > Convert.ToDateTime("1/1/2000")) { //****************************************************************************************************** //*** Dropbox Part //****************************************************************************************************** //*** Check dropBox Aurhentication Token if (Session["dropBoxAccessToken"] == null) { //*************************** //*** access token is empty //*************************** //*** 1. Check first for dropBox App Key & App secret if (String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["dropBoxAppKey"]) || String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["dropBoxAppSecret"])) { //*** Show Error Message Session["lblDropBoxMsg"] = "Please set app key and secret in this project's Web.config file and restart. " + "App key/secret can be found in the Dropbox App Console, here: " + "https://www.dropbox.com/developers/apps"; goto lnIndexEnd; } else //*** AppKey & secret exist { //*** Show Connect to DropBox button Session["btnDropBoxbtnV"] = "visible"; } } else { //*** Get DropBox Client Object Session["dropBoxClientObj"] = DropBoxConnector.getDropboxClient(Session["dropBoxAccessToken"].ToString()); if (DropBoxConnector.MsgError != "") //*** If error { Session["lblDropBoxMsg"] = "Dropbox Error: " + DropBoxConnector.MsgError; } else { //****************************************************************************************************** //*** Exact Online Part //****************************************************************************************************** //*** Adjust Drop Box Call Back URL for if (Session["exactOnlineReturnBackURL"] == null) { Session["exactOnlineReturnBackURL"] = Request.Url.Scheme + "://" + Request.Url.Host; if (Request.Url.Port > 0) { Session["exactOnlineReturnBackURL"] += ":" + Request.Url.Port.ToString(); } ; Session["exactOnlineReturnBackURL"] += "/" + System.Configuration.ConfigurationManager.AppSettings["exactOnlineReturnPage"]; } //*** Check If Code returned into Connection String if (Session["ExactOnlineAccessToken"] == null && Session["ExactOnlineReturnCode"] != null) { //**** Initialize Session Folder Path List <string> Dump = new List <string> { }; Session["ExactOnlineFolderPath"] = Dump; //**** Construct Exact Online Class ExactOnlineConnector objExactOnlineConnector = new ExactOnlineConnector(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"], System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"], new Uri(Session["exactOnlineReturnBackURL"].ToString()), Session["ExactOnlineReturnCode"].ToString()); Session["ExactOnlineAccessToken"] = objExactOnlineConnector.GetAccessToken(); if (objExactOnlineConnector.MsgError != "") { //*** If Error returned Session["lblDropBoxMsg"] = "Exact Online Error: " + objExactOnlineConnector.MsgError; goto lnIndexEnd; } } //*** Check ExactOnline Aurhentication Token if (Session["ExactOnlineAccessToken"] == null && Session["ExactOnlineReturnCode"] == null) { //*************************** //*** access token is empty //*************************** //*** 1. Check first for ExactOnline App Key & App secret if (String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientId"]) || String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["exactOnlineClientSecret"]) || String.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["exactOnlineEndPoint"])) { //*** Show Error Message Session["lblDropBoxMsg"] = "Please set client id, client secret and end point URL into this project's Web.config file and restart. " + "client id/secret can be found in the ExactOnline App Console, here: " + "https://start.exactonline.co.uk"; goto lnIndexEnd; } else //*** AppKey & secret exist { //*** Show Connect to ExactOnline button Session["btnExactOnlinebtnV"] = "visible"; } } if (Session["ExactOnlineReturnCode"] != null) { //*** 1. List all Dropbox Files await getDropBoxFilesList((List <string>) Session["FolderPath"]); //*** 2. Call Sync Function which Sync all Modified files with Exact Online Store await SyncAllFilesFolders((List <DropBoxFile>) Session["lstDropBoxFile"]); if (objRecord != null) { //*** Update Flag of WebHook Table objRecord.DW_Processed = 1; objRecord.DW_ProcessTimeStamp = DateTime.Now; objCloudStorageEntities.SaveChanges(); } } } } } lnIndexEnd: //*** GC objCloudStorageEntities.Dispose(); } HttpContext.Application["SyncInProgress"] = false; //*** Release Lock Flag //*** Display Results if (Session["SyncAllNumbers"].ToString() != "") { string[] strAllResultNumbers = Session["SyncAllNumbers"].ToString().Split(','); Session["ResultNumbers"] = "Dropbox Files= " + strAllResultNumbers[0] + " ,Files Processed= " + strAllResultNumbers[1] + " ,Files Replaced Successfully= " + strAllResultNumbers[2] + " ,Failure= " + strAllResultNumbers[3]; } try { return(View()); } finally { } }