public static void setLoggedUser(this HttpContext context, User userAccount, bool saveCookie = false) { DBcon dbContext = context.RequestServices.GetService <DBcon>(); string oldToken = context.Request.GetCookieJson <string>(loggedUser); if (oldToken != null) { Token remove = dbContext.Tokens.FirstOrDefault(i => i.Value == oldToken); if (remove != null) { dbContext.Tokens.Remove(remove); dbContext.SaveChanges(); } } if (userAccount != null) { string token = Guid.NewGuid().ToString(); dbContext.Tokens.Add(new Token { Value = token, UserId = userAccount.Id, Created = DateTime.Now }); dbContext.SaveChanges(); context.Response.SetCookieJson(loggedUser, token); } }
public IActionResult SendConfirmation(ForgotPasswordVM model) { if (!ModelState.IsValid) { return(RedirectToAction("ForgotPassword")); } User user = con.Users.SingleOrDefault(i => i.Email == model.Email); if (user == null) { TempData["errorMessage"] = "Email address doesn't exist. Make sure that you enter a valid email address."; return(RedirectToAction("ForgotPassword")); } ChangePasswordCode changepw = con.ChangePasswords.SingleOrDefault (i => i.UserId == user.Id); if (changepw != null) { if ((DateTime.Now - changepw.Created).TotalHours < 24) { TempData["errorMessage"] = "Email has been already sent to this email address"; return(RedirectToAction("ForgotPassword")); } else { con.ChangePasswords.Remove(changepw); con.SaveChanges(); } } string value = RandomString.GetString(30); string link = $"{ this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}/Login/ChangePassword?value=" + value; string message = "Visit this link for password change: \n" + link + "\nIf you don't change your password in next 24 hours this link will disappear " + "will be invalid."; EmailSettings.SendEmail(_configuration, user.Username, user.Email, "Change password", message); ChangePasswordCode passwordRequest = new ChangePasswordCode { Value = value, UserId = user.Id, Created = DateTime.Now }; con.ChangePasswords.Add(passwordRequest); con.SaveChanges(); TempData["successMessage"] = "Email for password confirmation is successfully sent. Check your inbox."; return(RedirectToAction("Index")); }
public IActionResult PaymentOption(PaymentFirstVM model) { if (HttpContext.GetLoggedUser() != null) { TempData["logged"] = "True"; } var user = HttpContext.GetLoggedUser(); var details = con.BillingDetails.Where(x => x.UserId == user.Id).FirstOrDefault(); BillingDetails billing = new BillingDetails(); if (model.CustomerInfo != null && details != null) { details.Email = model.CustomerInfo.Email; details.Fullname = model.CustomerInfo.FullName; details.Zip = model.CustomerInfo.Zip; details.Country = model.CustomerInfo.Country; details.City = model.CustomerInfo.City; details.StreetAddress = model.CustomerInfo.StreetAddress; details.UserId = user.Id; details.PhoneNumber = model.CustomerInfo.PhoneNumber; con.SaveChanges(); } else { billing.Email = model.CustomerInfo.Email; billing.Fullname = model.CustomerInfo.FullName; billing.Zip = model.CustomerInfo.Zip; billing.Country = model.CustomerInfo.Country; billing.City = model.CustomerInfo.City; billing.StreetAddress = model.CustomerInfo.StreetAddress; billing.UserId = user.Id; billing.PhoneNumber = model.CustomerInfo.PhoneNumber; con.Add(billing); } con.SaveChanges(); int product = con.Products.Find(model.ProductId).Id; if (!ModelState.IsValid) { return(RedirectToAction("Index", "HomePage")); } PaymentOptionVM pay = new PaymentOptionVM { Price = model.Price, ProductName = model.ProductName, PaypalRoute = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&amount=" + model.Price.ToString() + "&[email protected]&item_name=" + model.ProductName + "&return=https://localhost:44342/Payment/PaymentSuccess?ProductId=" + product }; return(View(pay)); }
public IActionResult SignUp(RegistrationIndexVM model) { if (!ModelState.IsValid) { return(View(model)); } if (con.Users.Any(u => u.Email == model.Email)) { TempData["errorMessage"] = "Email address is already in use"; return(RedirectToAction("Index")); } byte[] pwSalt = HashHelper.GetSalt(); string pwHash = HashHelper.GetHash(model.Password, pwSalt); User user = new User { Email = model.Email, Username = model.Username, PasswordHash = pwHash, PasswordSalt = Convert.ToBase64String(pwSalt), GenderId = 1 }; con.Users.Add(user); con.SaveChanges(); TempData["successMessage"] = "You are a registered user now."; return(RedirectToAction("Index", "Login")); }
public BlogPostCustom AddBlogPost(CreateBlogPostCustom addBlogPost) { if (addBlogPost != null) { string makeSlugFromTitle = null; makeSlugFromTitle = SlugRefactoring.Refactor(addBlogPost.blogPost.title); //if slug already exists in database return an empty object if (con.BlogPosts.Where(x => x.Slug == makeSlugFromTitle).Any()) { return(new BlogPostCustom("Object with this slug already exists")); } var model = new BlogPost() { Slug = makeSlugFromTitle, Title = addBlogPost.blogPost.title, Description = addBlogPost.blogPost.description, Body = addBlogPost.blogPost.body, CreatedAt = DateTime.Now, UpdatetAt = DateTime.Now }; con.Add(model); con.SaveChanges(); //if new object has tags if (addBlogPost.blogPost.tagList != null) { foreach (var tag in addBlogPost.blogPost.tagList) { var newTag = new Tag { TagName = tag }; //if tag doesn't exist in database, add a new tag in a tag table if (!con.Tags.Where(x => x.TagName == tag).Any()) { con.Add(newTag); con.SaveChanges(); } // if exists, then find id and add in many-to-many table (BlogPostTag) else { newTag.Id = con.Tags.Where(x => x.TagName == tag).FirstOrDefault().Id; } var blogtag = new BlogPostTag(); blogtag.BlogPostId = model.Id; blogtag.TagId = newTag.Id; con.Add(blogtag); con.SaveChanges(); } } //custom object for json return var bpc = new BlogPostCustom.BPCblog() { title = addBlogPost.blogPost.title, description = addBlogPost.blogPost.description, body = addBlogPost.blogPost.body, slug = makeSlugFromTitle, createdAt = DateTime.Now, updatedAt = DateTime.Now, tagList = addBlogPost.blogPost.tagList }; var bpcM = new BlogPostCustom() { blogPost = bpc }; return(bpcM); } return(new BlogPostCustom("Object provided as a parameter is null")); }