/// <summary> /// Retrieves the OrderID from the cookie if available; otherwise a -1 is returned /// </summary> /// <returns>OrderID if found in cookie; otherwise a -1 is returned.</returns> private int GetOrderIDFromCookie() { int orderID = Null.NullInteger; HttpCookie cookie = HttpContext.Current.Request.Cookies[CookieKey]; if ((cookie != null) && (cookie["OrderID"] != null)) { string cookieValue = cookie["OrderID"]; if (string.IsNullOrEmpty(cookieValue) == false) { int value; // If it's NOT an integer, try to decrypt the value if (int.TryParse(cookieValue, out value) == false) { string decrypted = SymmetricHelper.Decrypt(cookieValue); orderID = int.Parse(decrypted); } else { orderID = value; } } } return(orderID); }
public void SendBaseMessage(BaseMessage message, IEndPoint endPoint) { if (message.MessageType.ShouldBeEncrypted()) { var client = realClient.ClientsById[message.RealDestinationId]; if (message.MessageType == MessageType.Data && realClient.ClientsById.ContainsKey(message.DestinationId)) { realClient.ClientsById[message.DestinationId].DataBytesSent += message.Payload.Length; } var key = client.MainKey; if (key == null) { realClient.MessageHandler.DisconnectClient(client.Id); throw new DnmpException($"Selected client key is null; Client Id: [{client.Id}]"); } message = new BaseMessage( SymmetricHelper.Encrypt(key, message.SecurityHash.Concat(message.Payload).ToArray()), message.MessageType, message.SourceId, message.DestinationId, message.RealSourceId, message.RealDestinationId, message.Guid, message.MessageFlags ); } if (realClient.ClientsById.ContainsKey(message.RealDestinationId)) { realClient.ClientsById[message.RealDestinationId].BytesSent += message.TotalLength; } SendRawBytes(message.GetBytes(), endPoint); }
private static string GetCartID(int portalID, bool secureCookie) { string cartID = null; // Get cart ID from cookie HttpCookie cartCookie = HttpContext.Current.Request.Cookies[CookieKey(portalID)]; if (cartCookie != null) { cartID = cartCookie["CartID"]; if (!string.IsNullOrEmpty(cartID) && secureCookie && SymmetricHelper.CanSafelyEncrypt) { try { cartID = SymmetricHelper.Decrypt(cartID); } catch (FormatException) { // Do nothing in this case it's probably throwed // because the CartID is not encrypted! } } else { // Verify if it's a valid GUID, otherwise try to decrypt the value if (!CheckGuid(cartID)) { cartID = SymmetricHelper.Decrypt(cartID); } } } // Do we need to verify? if (!string.IsNullOrEmpty(cartID) && !_isCartVerified) { _isCartVerified = (Controller.GetCart(cartID, portalID) != null); if (!_isCartVerified) { cartID = null; } } // Do we need to create a new cart? if (string.IsNullOrEmpty(cartID)) { cartID = CreateCart(portalID); SetCartID(portalID, cartID, secureCookie); } return(cartID); }
private void SetOrderIdCookie(int orderID) { HttpCookie cookie = HttpContext.Current.Request.Cookies[CookieKey] ?? new HttpCookie(CookieKey); string cookieValue; if (StoreSettings.SecureCookie && SymmetricHelper.CanSafelyEncrypt) { cookieValue = SymmetricHelper.Encrypt(orderID.ToString()); } else { cookieValue = orderID.ToString(); } cookie["OrderID"] = cookieValue; HttpContext.Current.Response.Cookies.Add(cookie); }
protected void grdDownloads_ItemDataBound(object sender, DataGridItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DownloadInfo downloadInfo = (DownloadInfo)e.Item.DataItem; // Product Title Label lblProductTitle = (Label)e.Item.FindControl("lblProductTitle"); lblProductTitle.Text = downloadInfo.ProductTitle; // Allowed Downloads int allowed = downloadInfo.AllowedDownloads; Label lblAllowed = (Label)e.Item.FindControl("lblAllowed"); if (allowed == Null.NullInteger) { lblAllowed.Text = _unlimitedText; } else { allowed = downloadInfo.AllowedDownloads * downloadInfo.Quantity; lblAllowed.Text = allowed.ToString(); } // Downloaded int downloaded = downloadInfo.Downloads; if (downloaded == Null.NullInteger) { downloaded = 0; } Label lblDownloaded = (Label)e.Item.FindControl("lblDownloaded"); lblDownloaded.Text = downloaded.ToString(); // Download link HtmlAnchor lnkDownload = (HtmlAnchor)e.Item.FindControl("lnkDownload"); if (allowed == Null.NullInteger || downloaded < allowed) { lnkDownload.HRef = string.Format(_downloadPath, Server.UrlEncode(SymmetricHelper.Encrypt(downloadInfo.OrderDetailID + "," + UserId))); lnkDownload.Attributes["OnClick"] = _refreshScript; lnkDownload.InnerText = _downloadText; } else { lnkDownload.Visible = false; } } }
public ConnectionRequestConfirmReplyMessage(byte[] data, IEndPointFactory endPointFactory, ISymmetricKey key) { var dataReader = new BigEndianBinaryReader(new MemoryStream(SymmetricHelper.Decrypt(key, data))); NewId = dataReader.ReadUInt16(); NewEndPoint = endPointFactory.DeserializeEndPoint(dataReader.ReadBytes(dataReader.ReadUInt16())); var clientCount = dataReader.ReadUInt16(); for (var i = 0; i < clientCount; i++) { Clients.Add(new DnmpNode { Id = dataReader.ReadUInt16(), ParentId = dataReader.ReadUInt16(), EndPoint = endPointFactory.DeserializeEndPoint(dataReader.ReadBytes(dataReader.ReadUInt16())), CustomData = dataReader.ReadBytes(dataReader.ReadUInt16()) }); } }
private static void SetCartID(int portalID, string cartID, bool secureCookie) { if (!string.IsNullOrEmpty(cartID)) { HttpCookie cartCookie = new HttpCookie(CookieKey(portalID)); if (secureCookie && SymmetricHelper.CanSafelyEncrypt) { cartCookie["CartID"] = SymmetricHelper.Encrypt(cartID); } else { cartCookie["CartID"] = cartID; } HttpContext.Current.Response.Cookies.Add(cartCookie); } else { HttpCookie cartCookie = new HttpCookie(CookieKey(portalID)); cartCookie.Expires = DateTime.Today.AddDays(-100); HttpContext.Current.Response.Cookies.Add(cartCookie); } }
public byte[] GetBytes() { var dataMemoryStream = new MemoryStream(); var dataWriter = new BigEndianBinaryWriter(dataMemoryStream); dataWriter.Write(NewId); var selfBuf = endPointFactory.SerializeEndPoint(NewEndPoint); if (selfBuf.Length > ushort.MaxValue) { throw new DnmpException("buf.Length larger then ushort"); } dataWriter.Write((ushort)selfBuf.Length); dataWriter.Write(selfBuf); dataWriter.Write((ushort)Clients.Count(x => x.Id != NewId)); foreach (var client in Clients) { if (client.Id == NewId) { continue; } dataWriter.Write(client.Id); dataWriter.Write(client.ParentId); var buf = endPointFactory.SerializeEndPoint(client.EndPoint); if (buf.Length > ushort.MaxValue) { throw new DnmpException("buf.Length larger then ushort"); } dataWriter.Write((ushort)buf.Length); dataWriter.Write(buf); dataWriter.Write((ushort)client.CustomData.Length); dataWriter.Write(client.CustomData); } return(SymmetricHelper.Encrypt(key, dataMemoryStream.ToArray())); }
private void ReceiveCallback(byte[] data, IEndPoint source) { try { var message = new BaseMessage(data); if (message.MessageType.ShouldBeEncrypted() && (!realClient.ClientsById.ContainsKey(message.MessageType.OnlyBroadcasted() ? message.RealSourceId : message.SourceId) || !message.MessageType.ShouldBeEncrypted())) { return; } if (message.MessageType.ShouldBeEncrypted()) { var realSourceId = message.MessageFlags.HasFlag(MessageFlags.IsRedirected) ? message.RealSourceId : message.SourceId; var client = realClient.ClientsById[realSourceId]; var key = client.MainKey; if (key == null) { throw new DnmpException("Selected client key is null"); } message.Payload = SymmetricHelper.Decrypt(key, message.Payload); message.ReceivedHash = message.Payload.Take(HashUtil.GetHashSize()).ToArray(); message.Payload = message.Payload.Skip(HashUtil.GetHashSize()).ToArray(); if (!message.SecurityHash.SequenceEqual(message.ReceivedHash)) { throw new DnmpException("Hash of packets is not equal"); } } lock (receivedReliableMessages) { if (message.MessageType.IsReliable() && !receivedReliableMessages.Contains(message.Guid)) { if (!message.Hash.SequenceEqual(message.RealHash)) { return; } receivedReliableMessages.Add(message.Guid); SendBaseMessage( new BaseMessage(new ReliableConfirmMessage(message.Guid), realClient.SelfClient?.Id ?? 0xFFFF, message.MessageFlags.HasFlag(MessageFlags.IsRedirected) ? message.RealSourceId : message.SourceId), source); } else if (message.MessageType.IsReliable()) { return; } } if (realClient.ClientsById.ContainsKey(message.SourceId) && realClient.ClientsById[message.SourceId].EndPoint.Equals(source)) { realClient.ClientsById[message.SourceId].BytesReceived += message.TotalLength; } if (message.MessageType == MessageType.ReliableConfirm) { var decodedMessage = new ReliableConfirmMessage(message.Payload); if (!reliableMessages.ContainsKey(decodedMessage.MessageId)) { return; } EventQueue.RemoveEvent(decodedMessage.MessageId); reliableMessages.TryRemove(decodedMessage.MessageId, out _); } else { realClient.MessageHandler.ProcessMessage(message, source); } } catch (Exception e) { logger.Error(e, "Exception on receivng message"); } }
void IHttpHandler.ProcessRequest(HttpContext context) { PortalSettings portalSettings = PortalController.Instance.GetCurrentPortalSettings(); UserInfo user = (UserInfo)context.Items["UserInfo"]; if (user != null && user.UserID != Null.NullInteger) { // Init params int currentUserID = user.UserID; int portalID = portalSettings.PortalId; string key = context.Request.QueryString["KEY"]; key = SymmetricHelper.Decrypt(key); string[] values = key.Split(','); int orderDetailID = int.Parse(values[0]); int userID = int.Parse(values[1]); ProductInfo product = null; // Get the requested order detail row OrderController orderControler = new OrderController(); OrderDetailInfo orderDetail = orderControler.GetOrderDetail(orderDetailID); if (orderDetail != null) { // Get the corresponding product ProductController productControler = new ProductController(); product = productControler.GetProduct(portalID, orderDetail.ProductID); } // If user authentication and product are valid if (currentUserID == userID && product != null) { // Is download allowed? if (product.AllowedDownloads == Null.NullInteger || orderDetail.Downloads < product.AllowedDownloads) { // Update download counter then download file int downloads = orderDetail.Downloads; if (downloads == Null.NullInteger) { downloads = 1; } else { downloads += 1; } orderControler.UpdateOrderDetail(orderDetail.OrderDetailID, orderDetail.OrderID, orderDetail.ProductID, orderDetail.Quantity, orderDetail.UnitCost, orderDetail.RoleID, downloads); IFileInfo file = FileManager.Instance.GetFile(product.VirtualFileID); FileManager.Instance.WriteFileToResponse(file, ContentDisposition.Attachment); } // The following code is NEVER reached when download succeed! } // Redirect to the product detail page or the store page StoreInfo storeInfo = StoreController.GetStoreInfo(portalSettings.PortalId); CatalogNavigation catNav = new CatalogNavigation { TabID = storeInfo.StorePageID }; if (product != null) { catNav.CategoryID = product.CategoryID; catNav.ProductID = product.ProductID; } context.Response.Redirect(catNav.GetNavigationUrl()); } else { // Try to authenticate the user then retry download string returnUrl = context.Request.RawUrl; int posReturn = returnUrl.IndexOf("?returnurl="); if (posReturn != Null.NullInteger) { returnUrl = returnUrl.Substring(0, posReturn); } returnUrl = "returnurl=" + context.Server.UrlEncode(returnUrl); if (portalSettings.LoginTabId != Null.NullInteger && context.Request["override"] == null) { context.Response.Redirect(Globals.NavigateURL(portalSettings.LoginTabId, "", returnUrl), true); } else { if (portalSettings.HomeTabId != Null.NullInteger) { context.Response.Redirect(Globals.NavigateURL(portalSettings.HomeTabId, "Login", returnUrl), true); } else { context.Response.Redirect(Globals.NavigateURL(portalSettings.ActiveTab.TabID, "Login", returnUrl), true); } } } }