PageRenderTime 3402ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/ASPX/PizzaApp/Account/Profile.aspx.cs

https://gitlab.com/CORP-RESELLER/rest-api-sample-app-dotnet
C# | 261 lines | 236 code | 21 blank | 4 comment | 24 complexity | d45ef58e92e39d2de1047c552e130cec MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Data;
  7. using System.Data.SQLite;
  8. using PayPal;
  9. using PayPal.Api.Payments;
  10. namespace PizzaApp
  11. {
  12. public partial class Profile : System.Web.UI.Page
  13. {
  14. #region Data
  15. DataAccessLayer dataAccessObject;
  16. private DataTable GetUser(string email)
  17. {
  18. DataTable datTable = new DataTable();
  19. StringBuilder sqliteQuerySelect = new StringBuilder();
  20. sqliteQuerySelect.Append("SELECT ");
  21. sqliteQuerySelect.Append("id, ");
  22. sqliteQuerySelect.Append("email, ");
  23. sqliteQuerySelect.Append("encrypted_password, ");
  24. sqliteQuerySelect.Append("sign_in_count, ");
  25. sqliteQuerySelect.Append("current_sign_in_at, ");
  26. sqliteQuerySelect.Append("last_sign_in_at, ");
  27. sqliteQuerySelect.Append("last_sign_in_ip, ");
  28. sqliteQuerySelect.Append("created_at, ");
  29. sqliteQuerySelect.Append("updated_at, ");
  30. sqliteQuerySelect.Append("credit_card_id, ");
  31. sqliteQuerySelect.Append("credit_card_description ");
  32. sqliteQuerySelect.Append("FROM users ");
  33. sqliteQuerySelect.Append("WHERE email = @email");
  34. SQLiteDataAdapter sqliteDataAdapterSelect = new SQLiteDataAdapter();
  35. sqliteDataAdapterSelect.SelectCommand = new SQLiteCommand();
  36. sqliteDataAdapterSelect.SelectCommand.Parameters.AddWithValue("@email", email);
  37. dataAccessObject = new DataAccessLayer();
  38. datTable = dataAccessObject.Select(sqliteQuerySelect.ToString(), sqliteDataAdapterSelect);
  39. return datTable;
  40. }
  41. private bool IsPasswordValid(string email, string password)
  42. {
  43. bool isValid = false;
  44. string decryptedPassword = null;
  45. DataTable datTable = GetUser(email);
  46. if (datTable != null && datTable.Rows.Count > 0)
  47. {
  48. var distinctRows = from DataRow dRow in datTable.Rows
  49. where dRow.Field<string>("email") == email
  50. select new { column1 = dRow["encrypted_password"] };
  51. if (distinctRows != null)
  52. {
  53. foreach (var row in distinctRows)
  54. {
  55. string encryptedPassword = row.column1.ToString();
  56. decryptedPassword = Secure.Decrypt(encryptedPassword);
  57. break;
  58. }
  59. }
  60. if (password.Trim().Equals(decryptedPassword.Trim()))
  61. {
  62. isValid = true;
  63. }
  64. }
  65. return isValid;
  66. }
  67. private bool Update(string email)
  68. {
  69. bool isSuccess = false;
  70. int rowsAffacted = 0;
  71. var newPassword = TextBoxNewPassword.Text.Trim();
  72. var confirmNewPassword = TextBoxConfirmNewPassword.Text.Trim();
  73. var encryptedNewPassword = Secure.Encrypt(newPassword);
  74. var signInCount = 0;
  75. var dateTimeNow = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
  76. var currentSignInAt = dateTimeNow;
  77. // Set last signed in IP Address from database
  78. var lastSignInAt = string.Empty;
  79. // Set first signed in IP Address from database
  80. var signInIPAddress = string.Empty;
  81. // Set current signed in IP Address
  82. var currentSignInIP = string.Empty;
  83. // Set last signed in IP Address from database
  84. var lastSignInIP = string.Empty;
  85. var createdAt = string.Empty;
  86. var updatedAt = dateTimeNow;
  87. DataTable datTable = GetUser(email);
  88. if (datTable != null && datTable.Rows.Count > 0)
  89. {
  90. var distinctRows = from DataRow dRow in datTable.Rows
  91. where dRow.Field<string>("email") == email
  92. select new { column1 = dRow["sign_in_count"], column2 = dRow["last_sign_in_at"], column3 = dRow["last_sign_in_ip"], column4 = dRow["created_at"] };
  93. if (distinctRows != null)
  94. {
  95. foreach (var row in distinctRows)
  96. {
  97. signInCount = Convert.ToInt32(row.column1.ToString());
  98. signInCount++;
  99. DateTime lastSignInAtDateTime = Convert.ToDateTime(row.column2);
  100. lastSignInAt = lastSignInAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
  101. lastSignInIP = Convert.ToString(row.column3);
  102. DateTime createdAtDateTime = Convert.ToDateTime(row.column4);
  103. createdAt = createdAtDateTime.ToString("yyyy-MM-dd hh:mm:ss.FFFFF");
  104. break;
  105. }
  106. }
  107. CreditCard credCard = CreateCreditCard();
  108. var creditCardID = credCard.id;
  109. var creditCardDescription = credCard.number;
  110. StringBuilder sqliteQueryUpdate = new StringBuilder();
  111. sqliteQueryUpdate.Append("UPDATE Users ");
  112. sqliteQueryUpdate.Append("SET ");
  113. sqliteQueryUpdate.Append("encrypted_password = @encrypted_password, ");
  114. sqliteQueryUpdate.Append("sign_in_count = @sign_in_count, ");
  115. sqliteQueryUpdate.Append("current_sign_in_at = @current_sign_in_at, ");
  116. sqliteQueryUpdate.Append("last_sign_in_at = @last_sign_in_at, ");
  117. sqliteQueryUpdate.Append("current_sign_in_ip = @current_sign_in_ip, ");
  118. sqliteQueryUpdate.Append("last_sign_in_ip = @last_sign_in_ip, ");
  119. sqliteQueryUpdate.Append("created_at = @created_at, ");
  120. sqliteQueryUpdate.Append("credit_card_id = @credit_card_id, ");
  121. sqliteQueryUpdate.Append("credit_card_description = @credit_card_description ");
  122. sqliteQueryUpdate.Append("WHERE ");
  123. sqliteQueryUpdate.Append("email = @email");
  124. SQLiteDataAdapter sqliteDataAdapterUpdate = new SQLiteDataAdapter();
  125. sqliteDataAdapterUpdate.UpdateCommand = new SQLiteCommand();
  126. sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@email", email);
  127. sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@encrypted_password", encryptedNewPassword);
  128. sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@sign_in_count", signInCount);
  129. sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@current_sign_in_at", currentSignInAt);
  130. sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@last_sign_in_at", lastSignInAt);
  131. sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@current_sign_in_ip", currentSignInIP);
  132. sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@last_sign_in_ip", lastSignInIP);
  133. sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@created_at", createdAt);
  134. sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@updated_at", updatedAt);
  135. sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@credit_card_id", creditCardID);
  136. sqliteDataAdapterUpdate.UpdateCommand.Parameters.AddWithValue("@credit_card_description", creditCardDescription);
  137. dataAccessObject = new DataAccessLayer();
  138. rowsAffacted = dataAccessObject.Update(sqliteQueryUpdate.ToString(), sqliteDataAdapterUpdate);
  139. }
  140. if (rowsAffacted > 0)
  141. {
  142. isSuccess = true;
  143. }
  144. return isSuccess;
  145. }
  146. private bool DataBind(string email)
  147. {
  148. bool isSuccess = false;
  149. DataTable datTable = GetUser(email);
  150. if (datTable != null && datTable.Rows.Count > 0)
  151. {
  152. if (datTable.Rows[0]["email"] != DBNull.Value)
  153. {
  154. TextBoxEmail.Text = Convert.ToString(datTable.Rows[0]["email"]);
  155. }
  156. string creditCardID = string.Empty;
  157. if (datTable.Rows[0]["credit_card_id"] != DBNull.Value)
  158. {
  159. creditCardID = Convert.ToString(datTable.Rows[0]["credit_card_id"]);
  160. }
  161. CreditCard crdtCard = CreditCard.Get(Api, creditCardID);
  162. TextBoxCurrentCreditCardNumber.Text = crdtCard.number.Trim();
  163. isSuccess = true;
  164. }
  165. return isSuccess;
  166. }
  167. #endregion
  168. #region PayPal
  169. private string AccessToken
  170. {
  171. get
  172. {
  173. string token = new OAuthTokenCredential
  174. (
  175. "EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM",
  176. "EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM",
  177. Configuration.GetConfiguration()
  178. ).GetAccessToken();
  179. return token;
  180. }
  181. }
  182. private APIContext Api
  183. {
  184. get
  185. {
  186. APIContext context = new APIContext(AccessToken);
  187. context.Config = Configuration.GetConfiguration();
  188. return context;
  189. }
  190. }
  191. private CreditCard CreateCreditCard()
  192. {
  193. CreditCard CrdtCard = null;
  194. CreditCard credCard = new CreditCard();
  195. credCard.number = TextBoxNewCreditCardNumber.Text.Trim();
  196. credCard.cvv2 = TextBoxNewCreditCardCVV2.Text.Trim();
  197. credCard.type = DropDownListNewCreditCardType.SelectedValue.ToString().Trim();
  198. credCard.expire_month = System.Convert.ToInt32(DropDownListNewCreditCardExpireMonth.SelectedValue.ToString().Trim());
  199. credCard.expire_year = System.Convert.ToInt32(DropDownListNewCreditCardExpireYear.SelectedValue.ToString().Trim());
  200. CrdtCard = credCard.Create(Api);
  201. return CrdtCard;
  202. }
  203. #endregion
  204. #region Event Handlers
  205. protected void Page_Init(Object sender, EventArgs e)
  206. {
  207. if (HttpContext.Current.User.Identity.IsAuthenticated)
  208. {
  209. var email = HttpContext.Current.User.Identity.Name.Trim();
  210. bool isSuccess = DataBind(email);
  211. }
  212. else
  213. {
  214. Response.Redirect("~/Account/SignIn.aspx");
  215. }
  216. }
  217. protected void Page_Load(object sender, EventArgs e)
  218. {
  219. }
  220. protected void ButtonUpdate_Click(object sender, EventArgs e)
  221. {
  222. bool isValid = IsPasswordValid(TextBoxEmail.Text.Trim(), TextBoxCurrentPassword.Text.Trim());
  223. if (isValid)
  224. {
  225. bool isSuccess = Update(TextBoxEmail.Text.Trim());
  226. if (isSuccess)
  227. {
  228. FormsAuthentication.RedirectFromLoginPage(TextBoxEmail.Text.Trim(), false);
  229. }
  230. }
  231. else
  232. {
  233. divAlertMessage.Visible = true;
  234. divAlertMessage.Attributes["class"] = "alert fade in alert-error";
  235. LabelAlertMessage.Text = "Invalid Email or Password.";
  236. }
  237. }
  238. #endregion
  239. }
  240. }