Advertisement
Guest User

SessionState

a guest
Feb 3rd, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.92 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Web;
  4. using System.Web.UI;
  5.  
  6. using System.Web.UI.WebControls;
  7. using System.Web.Security;
  8. using System.Data;
  9. using System.Configuration;
  10. using System.Security.Principal;
  11. using BinOp.BusinessLogic.Web;
  12. using BinOp.BusinessLogic.Operations;
  13. using BinOp.Web.Security;
  14. using BinOp.Web;
  15. using BinOp.Core;
  16. using BinOp.Site.Code.Payments.Parameters;
  17. using System.Web.SessionState;
  18.  
  19. namespace BinOp.Site.Code
  20. {
  21. public class BasePage : Page
  22. {
  23. private const string USER_INFO_KEY = "USER_INFO";
  24. private const string DEFAULT_AUTHENTIFICATION_KEY = "DefaultAuthentificationPassed";
  25. private const string USER_AGENT = "http_user_agent";
  26. private const string SAFARI = "safari";
  27. private const string UPLEVEL = "uplevel";
  28. private const string TRUE = "true";
  29. private const string COLUMN_USER_ID = "UserID";
  30. private const string COLUMN_FIRST_NAME = "FirstName";
  31. private const string COLUMN_LAST_NAME = "LastName";
  32. private const string COLUMN_EMAIL = "Email";
  33. private const string COLUMN_PHONENUMBER = "PhoneNumber";
  34. private const string COLUMN_CHAT_BLOCKED = "ChatBlocked";
  35. private const string DEFAULT_PAGE_KEY = "DefaultPage";
  36. private const string LANGUAGE_DROPDOWN_ID = "ddlLanguage";
  37. private const string EVENT_TARGET = "__EVENTTARGET";
  38. private const string DEFAULT_VOUCHER = "DefaultVoucher";
  39. private const string SSL_PORT_KEY = "SslPort";
  40. private const string HTTP_PORT_KEY = "HttpPort";
  41. private const string COLUMN_BIRTH_DATE = "BirthDate";
  42.  
  43. public string MainDepositPageURl { get; set; }
  44.  
  45. public string PaymentProviderName { get; set; }
  46.  
  47. protected class CustomErrorMessage : IValidator
  48. {
  49. private string Message;
  50.  
  51. public CustomErrorMessage(string message)
  52. {
  53. this.Message = message;
  54. }
  55.  
  56. public string ErrorMessage { get { return Message; } set { Message = value; } }
  57. public bool IsValid { get { return false; } set { } }
  58. public void Validate() { }
  59. }
  60.  
  61. public BasePage()
  62. {
  63. const string SSL_ON_PAGES_KEY = "SslOnPages";
  64.  
  65. //Verify ssl
  66. var attribute = this.GetType().GetCustomAttributes(typeof(RequireSSLAttribute), true).FirstOrDefault() as RequireSSLAttribute;
  67. //Get SslOnPages from configuration to override attributes
  68. var sslOnPages = ConfigUtilities.GetAppSettingsBoolean(SSL_ON_PAGES_KEY);
  69.  
  70. if (HttpContext.Current.Request.Url.Scheme == Uri.UriSchemeHttps && (attribute == null || !attribute.RequireSSL) && sslOnPages)
  71. {
  72. //Redirect to http
  73. var httpPort = ConfigUtilities.GetAppSettingsInt(HTTP_PORT_KEY);
  74. WebUtils.RedirectToHttp(this.Context, httpPort);
  75. return;
  76. }
  77. else if (HttpContext.Current.Request.Url.Scheme == Uri.UriSchemeHttp && (attribute != null && attribute.RequireSSL) && sslOnPages)
  78. {
  79. //Redirect to https
  80. var sslPort = ConfigUtilities.GetAppSettingsInt(SSL_PORT_KEY);
  81. WebUtils.RedirectToSsl(this.Context, sslPort);
  82. return;
  83. }
  84.  
  85. //Verify role
  86. object[] attributes = this.GetType().GetCustomAttributes(typeof(RequiredRoleAttribute), true);
  87. if (attributes.Length > 0)
  88. {
  89. IPrincipal user = HttpContext.Current.User;
  90. if (user != null)
  91. {
  92. RequiredRoleAttribute roleAttribute = null;
  93. for (int i = 0; i < attributes.Length; i++)
  94. {
  95. roleAttribute = attributes[i] as RequiredRoleAttribute;
  96. if (!user.IsInRole(roleAttribute.Role))
  97. {
  98. FormsAuthentication.RedirectToLoginPage();
  99. }
  100. }
  101. }
  102. else
  103. FormsAuthentication.RedirectToLoginPage();
  104. }
  105.  
  106. string affid = HttpContext.Current.Request.Params.Get("AFFID");
  107. if (affid != null)
  108. {
  109. HttpCookie aCookie = new HttpCookie("userInfo");
  110. aCookie.Values["AFFID"] = affid;
  111. aCookie.Values["lastVisit"] = DateTime.Now.ToString();
  112. HttpContext.Current.Response.Cookies.Add(aCookie);
  113. }
  114.  
  115. //logic for refer a friend functionality to store the referal id in a cookie
  116. string referalId = HttpContext.Current.Request.Params.Get("REF");
  117. if (referalId != null)
  118. {
  119. Session["REF"] = referalId; //???
  120. HttpCookie refCookie = new HttpCookie("refInfo");
  121. refCookie.Values["REF"] = referalId;
  122. refCookie.Values["lastVisit"] = DateTime.Now.ToString();
  123. HttpContext.Current.Response.Cookies.Add(refCookie);
  124. }
  125.  
  126. this.SetProviderName();
  127. }
  128.  
  129. public UserDescriptor UserInfo
  130. {
  131. get
  132. {
  133. object info = Session[USER_INFO_KEY];
  134. if (info != null)
  135. return info as UserDescriptor;
  136. return null;
  137. }
  138. set
  139. {
  140. Session[USER_INFO_KEY] = value;
  141. }
  142. }
  143.  
  144. protected void ShowCustomValidationMessage(string message)
  145. {
  146. Validators.Add(new CustomErrorMessage(message));
  147. }
  148.  
  149. protected override void OnPreInit(EventArgs e)
  150. {
  151. if (HttpContext.Current.User.Identity.IsAuthenticated)
  152. {
  153. if (UserInfo == null || HttpContext.Current.User.Identity.Name != UserInfo.EMail)
  154. {
  155. Helper.LoginUserByEmail(HttpContext.Current.User.Identity.Name, null);
  156. }
  157. }
  158. else
  159. {
  160. UserInfo = null;
  161. Session["HideTradeBoxExcept"] = null;
  162. }
  163.  
  164. if (!String.IsNullOrEmpty(Page.Request.ServerVariables[USER_AGENT]))
  165. {
  166. string servVar = Page.Request.ServerVariables[USER_AGENT];
  167. if (servVar.ToLower().Contains(SAFARI))
  168. Page.ClientTarget = UPLEVEL;
  169. }
  170. base.OnPreInit(e);
  171. }
  172.  
  173. protected override void OnLoad(EventArgs e)
  174. {
  175. if (User.Identity.IsAuthenticated && UserInfo != null)
  176. {
  177. UsersOperations.SetLastActivity(UserInfo.UserID);
  178. }
  179.  
  180. base.OnLoad(e);
  181.  
  182. }
  183.  
  184. public string CurrencySign
  185. {
  186. get
  187. {
  188. if (UserInfo != null && !string.IsNullOrEmpty(UserInfo.CurrencySign))
  189. {
  190. return UserInfo.CurrencySign;
  191. }
  192.  
  193. return ConfigurationManager.AppSettings["DefaultCurrencySymbol"] ?? "$";
  194. }
  195. }
  196.  
  197. /*
  198. public UserDescriptor FillUserData(object ResultValue)
  199. {
  200. DataTable userData = ResultValue as DataTable;
  201.  
  202. if (userData != null && userData.Rows.Count == 0) return null;
  203.  
  204. int userID = Convert.ToInt32(userData.Rows[0][COLUMN_USER_ID]);
  205. string firstName = Convert.ToString(userData.Rows[0][COLUMN_FIRST_NAME]);
  206. string lastName = Convert.ToString(userData.Rows[0][COLUMN_LAST_NAME]);
  207. string email = Convert.ToString(userData.Rows[0][COLUMN_EMAIL]);
  208. string phoneNumber = Convert.ToString(userData.Rows[0][COLUMN_PHONENUMBER]);
  209. int chatBlocked = Convert.ToInt32(userData.Rows[0][COLUMN_CHAT_BLOCKED]);
  210. DateTime BirthDate = Convert.ToDateTime(userData.Rows[0][COLUMN_BIRTH_DATE]);
  211.  
  212. return new UserDescriptor(userID, firstName, lastName, email, phoneNumber, chatBlocked, BirthDate);
  213. }
  214. */
  215.  
  216. private bool DefaultAuthentificationPassed
  217. {
  218. get
  219. {
  220. if (Session[DEFAULT_AUTHENTIFICATION_KEY] != null)
  221. return Convert.ToBoolean(Session[DEFAULT_AUTHENTIFICATION_KEY]);
  222. return false;
  223. }
  224. set
  225. {
  226. Session[DEFAULT_AUTHENTIFICATION_KEY] = value;
  227. }
  228. }
  229.  
  230. public string DefaultPage
  231. {
  232. get
  233. {
  234. return ConfigurationManager.AppSettings[DEFAULT_PAGE_KEY];
  235. }
  236. }
  237.  
  238. protected bool IsLanguageChangeRequest
  239. {
  240. get
  241. {
  242. string targetId = Context.Request.Form[EVENT_TARGET];
  243. if (!String.IsNullOrEmpty(targetId))
  244. return targetId.EndsWith(LANGUAGE_DROPDOWN_ID);
  245. return false;
  246. }
  247. }
  248.  
  249. protected int DefaultVoucherValue
  250. {
  251. get
  252. {
  253. int val = 0;
  254. if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings[DEFAULT_VOUCHER]))
  255. int.TryParse(ConfigurationManager.AppSettings[DEFAULT_VOUCHER], out val);
  256. return val;
  257. }
  258. }
  259.  
  260. protected override void OnPreRender(EventArgs e)
  261. {
  262. MasterPage master = Master;
  263. while (master != null && !(master is Site.masterpages.root))
  264. master = master.Master;
  265. if (master != null) (master as Site.masterpages.root).HideVoucher(DefaultVoucherValue);
  266. master = Master;
  267. while (master != null && !(master is Site.masterpages.navigation))
  268. master = master.Master;
  269. if (master != null) (master as Site.masterpages.navigation).HideVoucher(DefaultVoucherValue);
  270. HideVouchers(DefaultVoucherValue);
  271.  
  272. base.OnPreRender(e);
  273. }
  274.  
  275. /// <summary>
  276. /// Get user's temo demo exiration date by ID
  277. /// </summary>
  278. /// <param name="userID">The current logged user ID</param>
  279. /// <returns>The current user temp demo expiration date or null</returns>
  280. protected DateTime? GetUserTempDemoExpiration(int userID)
  281. {
  282. OperationResult result = UsersOperations.GetUserTempDemoExpiration(userID);
  283. if (result.ProcessedSuccessful)
  284. {
  285. DateTime tempDemoExpirationDate = new DateTime();
  286. if (DateTime.TryParse(result.ResultValue.ToString(), out tempDemoExpirationDate))
  287. {
  288. return tempDemoExpirationDate;
  289. }
  290. }
  291.  
  292. return null;
  293. }
  294.  
  295. /// <summary>
  296. /// Redirects the user to the default login page if the session parameter UserInfo is empty or the user is not authenticated
  297. /// </summary>
  298. protected void HandleNotLoggedInUsers()
  299. {
  300. if (UserInfo == null || !User.Identity.IsAuthenticated)
  301. {
  302. Response.Redirect("~/trading");
  303. }
  304.  
  305. }
  306.  
  307. protected virtual void HideVouchers(int voucher)
  308. {
  309. }
  310.  
  311. protected void SetProviderName()
  312. {
  313. this.PaymentProviderName = ConfigurationManager.AppSettings["ChargeProvider"];
  314.  
  315. if (!string.IsNullOrEmpty(this.PaymentProviderName))
  316. {
  317. switch (this.PaymentProviderName)
  318. {
  319. case PaymentTypeConstants.SOLID_3D:
  320. case PaymentTypeConstants.CASHU:
  321. this.MainDepositPageURl = "SimpleDepositForm";
  322. break;
  323. default:
  324. this.MainDepositPageURl = "Deposit";
  325. break;
  326. }
  327. }
  328. }
  329.  
  330. protected void RedirectToProperPaymentProviderPage(string commingFrom)
  331. {
  332. if (this.MainDepositPageURl != commingFrom && this.MainDepositPageURl != "SimpleDepositForm")
  333. {
  334. Response.Redirect(this.MainDepositPageURl);
  335. }
  336. }
  337.  
  338. protected string GetUserCurrencyNameForUser()
  339. {
  340. return CurrencyHelper.GetUserCurrencyNameForUser(UserInfo.UserID);
  341. }
  342.  
  343. /// <summary>
  344. /// Create additional parameters associated with a user
  345. /// </summary>
  346. protected void CreateAdditionalParameters(int userID)
  347. {
  348. string name = string.Empty;
  349. string value = string.Empty;
  350.  
  351. string externParametersString = ConfigurationManager.AppSettings["ExternalParameters"];
  352. if (!string.IsNullOrEmpty(externParametersString))
  353. {
  354. string[] externalParameterNames = externParametersString.Split(',');
  355. string externalContainerName = ConfigurationManager.AppSettings["ExternalContainerName"];
  356.  
  357. string currentExternalParameterName = string.Empty;
  358. string currentExternalParameterValue = string.Empty;
  359.  
  360. foreach (string externalParameterName in externalParameterNames)
  361. {
  362. currentExternalParameterName = externalParameterName.Trim();
  363.  
  364. if (!string.IsNullOrEmpty(externalContainerName))
  365. {
  366. HttpCookie currentCookie = Request.Cookies[externalContainerName];
  367.  
  368. // get the value from a cookie
  369. if (currentCookie != null && currentCookie[currentExternalParameterName] != null)
  370. {
  371. value = currentCookie[currentExternalParameterName];
  372. name = currentExternalParameterName;
  373. }
  374. else // check the session for a parameter value
  375. {
  376. if (Session[currentExternalParameterName] != null)
  377. {
  378. value = Session[currentExternalParameterName].ToString();
  379. name = currentExternalParameterName;
  380. }
  381. else
  382. {
  383. Session.Add(currentExternalParameterName, currentExternalParameterValue);
  384. }
  385. }
  386.  
  387. // save into the database
  388. if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
  389. {
  390. ExternalParametersOperations.Save(userID, name, value);
  391. }
  392. }
  393. }
  394. }
  395. }
  396. }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement