protected void btnRegister_Click(object sender, EventArgs e) { var userStore = new UserStore <IdentityUser>(); var manager = new UserManager <IdentityUser>(userStore); var user = new IdentityUser() { UserName = txtRegisterUsername.Text }; IdentityResult result = manager.Create(user, txtRegisterPassword.Text); if (result.Succeeded) { DatabaseCO5027Entities db = new DatabaseCO5027Entities(); var userDetails = new UserDetail(); userDetails.UserId = user.Id; userDetails.FirstName = txtRegisterFirstName.Text; userDetails.Surname = txtRegisterSurname.Text; userDetails.Email = txtRegisterEmail.Text; db.UserDetails.Add(userDetails); db.SaveChanges(); db.Dispose(); SendWelcomeEmailToCustomer(user); SendWelcomeEmailToAdmin(user); Login(user, manager); } else { litError.Text = result.Errors.FirstOrDefault(); } }
protected void btnUpload_Click(object sender, EventArgs e) { decimal price; if (!Decimal.TryParse(txtPrice.Text, out price)) { litFeedback.Text = "Please enter a price in the formal: 5.20"; return; } string idString = Request.QueryString["id"]; int id = 0; if (int.TryParse(idString, out id)) { DatabaseCO5027Entities db = new DatabaseCO5027Entities(); var product = db.Products.Single(p => p.Id == id); product.Name = txtName.Text; product.Description = txtDescription.Text; product.Price = price; db.SaveChanges(); Response.Redirect("~/admin"); } else { if (uploadImage(txtName.Text, txtDescription.Text, price)) { txtName.Text = ""; txtDescription.Text = ""; Response.Redirect("~/admin"); } } }
protected void Login(IdentityUser user, UserManager <IdentityUser> manager) { var authenticationManager = HttpContext.Current.GetOwinContext().Authentication; var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity); // add items to basket if necessary if (Session["basketProductId"] != null) { DatabaseCO5027Entities db = new DatabaseCO5027Entities(); var basketEntry = new Basket(); basketEntry.CustomerId = user.Id; basketEntry.ProductId = (int)Session["basketProductId"]; db.Baskets.Add(basketEntry); db.SaveChanges(); Session.Remove("basketProductId"); Response.Redirect("~/checkout.aspx"); } if (Request.QueryString["ReturnUrl"] == null) { Response.Redirect("~/user/default.aspx"); } }
protected void rptOrderedProducts_ItemCommand(object source, RepeaterCommandEventArgs e) { string idString = e.CommandArgument.ToString(); int id = int.Parse(idString); DatabaseCO5027Entities db = new DatabaseCO5027Entities(); var orderedProduct = db.OrderedProducts.Single(op => op.Id == id); orderedProduct.DownloadsAllowed += 1; db.SaveChanges(); BindRepeater(); }
protected void rptOrders_ItemCommand(object source, RepeaterCommandEventArgs e) { string idString = e.CommandArgument.ToString(); int id = int.Parse(idString); DatabaseCO5027Entities db = new DatabaseCO5027Entities(); var order = db.Orders.Single(o => o.Id == id); order.Cancelled = true; foreach (var item in order.OrderedProducts) { item.DownloadsAllowed = 0; } db.SaveChanges(); BindRepeater(); }
protected void Page_Load(object sender, EventArgs e) { // fetch photo id int id = 0; bool idIsInt = int.TryParse(Request.QueryString["id"], out id); if ((!idIsInt || id == 0)) { Response.Redirect("~/user/"); } DatabaseCO5027Entities db = new DatabaseCO5027Entities(); OrderedProduct orderedProduct = new OrderedProduct(); string userId = User.Identity.GetUserId(); int remainingDownloads = 0; try { var orderedProducts = db.OrderedProducts.Where(op => op.Order.CustomerId == userId && op.ProductId == id); bool downloadAllowed = false; foreach (OrderedProduct item in orderedProducts) { if ((item.DownloadCount < item.DownloadsAllowed)) { downloadAllowed = true; orderedProduct = item; remainingDownloads += (item.DownloadsAllowed - item.DownloadCount); } } if (!downloadAllowed) { litMessage.Text = "<p>You are not permitted to download this photo. If you think this message is in error, please <a href=\"" + ResolveUrl("~/contact.aspx") + "\">contact us.</a></p>"; return; } } catch { Response.Redirect("~/user/"); } // fetch from db Product photo = orderedProduct.Product; string extention = photo.Extension; int imageSize = (int)photo.SizeOfFile; string downloadName = photo.Name + extention; string fileLocation = MapPath("~/files/images/original/" + id.ToString() + extention); // count download orderedProduct.DownloadCount += 1; remainingDownloads -= 1; db.SaveChanges(); sendEmailToCustomer(orderedProduct, remainingDownloads); try { // initiate download Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "image/jpeg"; Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadName + ";"); Response.AddHeader("Content-Length", imageSize.ToString()); Response.WriteFile(fileLocation); Response.Flush(); Response.End(); } catch { } }
private bool uploadImage(string name, string description, decimal price) { bool success = false; string fileExtention = System.IO.Path.GetExtension(fUplPictureUpload.FileName).ToLower(); if (fileExtention == ".jpeg" || fileExtention == ".jpg" || fileExtention == ".gif" || fileExtention == ".png" || fileExtention == ".tif" || fileExtention == ".tiff") { try { // check image is readable & determine dimentions System.Drawing.Image img = System.Drawing.Image.FromStream(fUplPictureUpload.PostedFile.InputStream); int height = img.Height; int width = img.Width; DatabaseCO5027Entities db = new DatabaseCO5027Entities(); Product product = new Product(); product.Archived = false; product.Name = name; product.Description = description; product.Price = price; product.InitialHeight = height; product.InitialWidth = width; product.Extension = fileExtention; db.Products.Add(product); db.SaveChanges(); string filename = product.Id.ToString(); // save original image to disk string filePath = Server.MapPath("~/files/images/original/" + filename + fileExtention); img.Save(filePath); product.SizeOfFile = (int)new System.IO.FileInfo(filePath).Length; db.SaveChanges(); // save watermarked images to disk ImageProcessing imageProcessor = new ImageProcessing(); if (imageProcessor.SaveWatermarkedImages(img, product.Id)) { success = true; return(success); } else { success = false; litFeedback.Text = "Unable to process image, please go to <a href='manage.aspx'>Admin Panel</a> and click 'Reprocess Images' to try again."; return(success); } } catch { success = false; litFeedback.Text = "Image not readable"; return(success); } } else { success = false; litFeedback.Text = "Images of " + fileExtention + " are not accepted. Please upload a JPEG, PNG, GIF or TIFF."; return(success); } }
public bool SaveWatermarkedImages(System.Drawing.Image img, int photoId) { bool success = false; string filename = photoId.ToString(); int height = img.Height; int width = img.Width; string path = System.Web.Hosting.HostingEnvironment.MapPath("~/files/images/watermarked/"); DatabaseCO5027Entities db = new DatabaseCO5027Entities(); img = AddWatermark(img); var sizes = db.Sizes.Where(s => s.Archived == false).ToList(); foreach (var size in sizes) { int maxWidth = size.MaxWidth; int maxHeight = size.MaxHeight; var resizedImage = img; // prevents enlarging if (width > maxWidth || height > maxHeight) { resizedImage = ImageManipulation.ResizeImage(img, maxWidth, maxHeight); } int newHeight = resizedImage.Height; int newWidth = resizedImage.Width; string filePath = path + filename + "-" + size.Id + ".jpg"; resizedImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg); var fileSize = new System.IO.FileInfo(filePath).Length; var imageInDb = size.Images.FirstOrDefault(i => i.ProductId == photoId); if (imageInDb != null) { imageInDb.Height = newHeight; imageInDb.Width = newWidth; imageInDb.SizeOfFile = (int)fileSize; db.SaveChanges(); } else { imageInDb = new CO5027.Image(); imageInDb.Height = newHeight; imageInDb.Width = newWidth; imageInDb.ProductId = photoId; imageInDb.SizeId = size.Id; imageInDb.SizeOfFile = (int)fileSize; db.Images.Add(imageInDb); db.SaveChanges(); } } success = true; return(success); }