public static Aspose_GmailSync_ServerDetails CheckGmailDetails(int userID) { Aspose_GmailSync_ServerDetails detailsToReturn = null; try { Aspose_GmailSync_ServerDetails serverDetails = CurrentDBEntities.Aspose_GmailSync_ServerDetails.FirstOrDefault(x => x.DNNUserID == userID); if (serverDetails != null) { detailsToReturn = new Aspose_GmailSync_ServerDetails(); detailsToReturn.DNNUserID = serverDetails.DNNUserID; detailsToReturn.Username = serverDetails.Username; detailsToReturn.Email = serverDetails.Email; detailsToReturn.Password = Crypto.Decrypt(serverDetails.Password); detailsToReturn.ClientID = Crypto.Decrypt(serverDetails.ClientID); detailsToReturn.ClientSecret = Crypto.Decrypt(serverDetails.ClientSecret); detailsToReturn.RefreshToken = Crypto.Decrypt(serverDetails.RefreshToken); } } catch (Exception) { // Fall back to session approach if database fails if (HttpContext.Current.Session[GmailDetailsSessionName(userID)] != null) { detailsToReturn = (Aspose_GmailSync_ServerDetails)HttpContext.Current.Session[GmailDetailsSessionName(userID)]; } } return detailsToReturn; }
public static void AddUpdateServerDetails(Aspose_GmailSync_ServerDetails details) { try { Aspose_GmailSync_ServerDetails serverDetails = CurrentDBEntities.Aspose_GmailSync_ServerDetails.FirstOrDefault(x => x.DNNUserID == details.DNNUserID); if (serverDetails != null) { serverDetails.DNNUserID = details.DNNUserID; serverDetails.Username = details.Username; serverDetails.Email = details.Email; serverDetails.Password = details.Password; serverDetails.ClientID = details.ClientID; serverDetails.ClientSecret = details.ClientSecret; serverDetails.RefreshToken = details.RefreshToken; CurrentDBEntities.SaveChanges(); } else { CurrentDBEntities.Aspose_GmailSync_ServerDetails.Add(details); CurrentDBEntities.SaveChanges(); } } catch (Exception) { // Fall back to session approach if database fails details.Password = Crypto.Decrypt(details.Password); HttpContext.Current.Session[GmailDetailsSessionName((int)details.DNNUserID)] = details; } }
protected void SaveButton_Click(object sender, EventArgs e) { GmailCredsErrorDiv.Visible = false; Aspose_GmailSync_ServerDetails serverDetails = new Aspose_GmailSync_ServerDetails(); serverDetails.Email = EmailAddressTextBox.Text.Trim(); if (serverDetails.Email.Contains("@")) { serverDetails.Username = serverDetails.Email.Split('@')[0]; } serverDetails.Password = PasswordTextBox.Text.Trim(); serverDetails.ClientID = ClientIDTextBox.Text.Trim(); serverDetails.ClientSecret = ClientSecretTextBox.Text.Trim(); serverDetails.DNNUserID = UserId; try { string refresh_token = string.Empty; //Code segment - START //This segment of code is used to get the refresh_token. In general, you do not have to refresh refresh_token every time, you need to do it once, and then use it to retrieve access-token. //Thus, use it once to retrieve the refresh_token and then use the refresh_token value each time. string access_token; string token_type; int expires_in; GoogleTestUser user = new GoogleTestUser(serverDetails.Username, serverDetails.Email, serverDetails.Password, serverDetails.ClientID, serverDetails.ClientSecret); GoogleOAuthHelper.GetAccessToken(user, out access_token, out refresh_token, out token_type, out expires_in); serverDetails.RefreshToken = refresh_token; //Code segment - END using (IGmailClient client = Aspose.Email.Google.GmailClient.GetInstance(serverDetails.ClientID, serverDetails.ClientSecret, serverDetails.RefreshToken)) { FeedEntryCollection groups = client.FetchAllGroups(); } } catch (Exception) { GmailCredsErrorDiv.Visible = true; return; } serverDetails.Password = Crypto.Encrypt(serverDetails.Password); serverDetails.ClientID = Crypto.Encrypt(serverDetails.ClientID); serverDetails.ClientSecret = Crypto.Encrypt(serverDetails.ClientSecret); serverDetails.RefreshToken = Crypto.Encrypt(serverDetails.RefreshToken); DatabaseHelper.AddUpdateServerDetails(serverDetails); ResetControls(); if (GmailToDnnClickedHiddenField.Value.Equals("true")) { GmailToDnnSync.Visible = true; GmailToDnnClickedHiddenField.Value = "false"; } else if (DnnToGmailClickedHiddenField.Value.Equals("true")) { DnnToGmailSync.Visible = true; DnnToGmailClickedHiddenField.Value = "false"; } }