/// <summary> /// Event handler for BeginRequest. /// </summary> /// <param name="sender">Sender object instance.</param> /// <param name="e">Event arguments.</param> void Context_BeginRequest(object sender, EventArgs e) { if (InstallerHelper.ConnectionStringIsSet()) { try { if (HttpContext.Current != null && !HttpContext.Current.Request.Url.IsLoopback) { HttpApplication application = sender as HttpApplication; BannedIpAddress clientIP = new BannedIpAddress(); clientIP.Address = application.Request.UserHostAddress; // On any unexpected error we let visitor to visit website if (IpBlacklistManager.IsIpAddressBanned(clientIP)) { // Blocking process // for now just show error 404 - Forbidden // later let the user know that his ip address/network // was banned and a reason why... this means we need an error page (aspx) application.Response.StatusCode = 403; application.Server.Transfer("~/BannedAddress.htm"); application.Response.StatusDescription = "Access is denied"; application.Response.End(); } } } catch (Exception exc) { } } }
/// <summary> /// Saves a BannedIpAddress /// </summary> /// <returns>BannedIpAddress</returns> public BannedIpAddress SaveBannedIpAddressInfo() { DateTime nowDT = DateTime.UtcNow; BannedIpAddress ipAddress = this.BlacklistService.GetBannedIpAddressById(this.BannedIpAddressId); // Check if the IP is valid if (!this.BlacklistService.IsValidIp(txtBannedIP.Text.Trim())) { throw new NopException("The following isn't a valid IP address: " + txtBannedIP.Text); } //if ip address is not null update if (ipAddress != null) { ipAddress.Address = txtBannedIP.Text; ipAddress.Comment = txtComment.Text; ipAddress.UpdatedOn = nowDT; this.BlacklistService.UpdateBannedIpAddress(ipAddress); } else //insert { ipAddress = new BannedIpAddress() { Address = txtBannedIP.Text, Comment = txtComment.Text, CreatedOn = nowDT, UpdatedOn = nowDT }; this.BlacklistService.InsertBannedIpAddress(ipAddress); } return(ipAddress); }
protected void OnSaveClick(object sender, EventArgs e) { if (Page.IsValid) { try { BannedIpAddress ipAddress = ctrlBlacklist.SaveBannedIpAddressInfo(); Response.Redirect("BlacklistIPDetails.aspx?BannedIpAddressID=" + ipAddress.BannedIpAddressID.ToString()); } catch (Exception exc) { ProcessException(exc); } } }
protected void SaveAndStayButton_Click(object sender, EventArgs e) { if (Page.IsValid) { try { BannedIpAddress ipAddress = Save(); Response.Redirect("BlacklistIPDetails.aspx?BannedIpAddressID=" + ipAddress.BannedIpAddressId.ToString()); } catch (Exception exc) { ProcessException(exc); } } }
protected void SaveButton_Click(object sender, EventArgs e) { if (Page.IsValid) { try { BannedIpAddress ipAddress = Save(); Response.Redirect("Blacklist.aspx"); } catch (Exception exc) { ProcessException(exc); } } }
/// <summary> /// Bind controls on the form /// </summary> private void BindData() { BannedIpAddress ipAddress = IpBlacklistManager.GetBannedIpAddressById(this.BannedIpAddressId); if (ipAddress != null) { txtBannedIP.Text = ipAddress.Address; txtComment.Text = ipAddress.Comment; this.pnlCreatedOn.Visible = true; this.pnlUpdatedOn.Visible = true; lblCreatedOn.Text = DateTimeHelper.ConvertToUserTime(ipAddress.CreatedOn, DateTimeKind.Utc).ToString(); lblUpdatedOn.Text = DateTimeHelper.ConvertToUserTime(ipAddress.UpdatedOn, DateTimeKind.Utc).ToString(); } else { this.pnlCreatedOn.Visible = false; this.pnlUpdatedOn.Visible = false; } }
/// <summary> /// Saves a BannedIpAddress /// </summary> /// <returns>BannedIpAddress</returns> public BannedIpAddress SaveBannedIpAddressInfo() { DateTime nowDT = DateTime.UtcNow; BannedIpAddress ipAddress = IpBlacklistManager.GetBannedIpAddressById(this.BannedIpAddressId); // Check if the IP is valid if (!IpBlacklistManager.IsValidIp(txtBannedIP.Text.Trim())) { throw new NopException("The following isn't a valid IP address: " + txtBannedIP.Text); } //if ip address is not null update if (ipAddress != null) { ipAddress = IpBlacklistManager.UpdateBannedIpAddress(this.BannedIpAddressId, txtBannedIP.Text, txtComment.Text, ipAddress.CreatedOn, nowDT); } else //insert { ipAddress = IpBlacklistManager.InsertBannedIpAddress(txtBannedIP.Text, txtComment.Text, nowDT, nowDT); } return(ipAddress); }
protected BannedIpAddress Save() { BannedIpAddress ipAddress = ctrlBlacklist.SaveBannedIpAddressInfo(); return(ipAddress); }