public async Task <IActionResult> OnPostAddCommentAsync(int id) { Post = await _context.Posts.Include(p => p.User).FirstOrDefaultAsync(m => m.PostId == id); //// ReCAPTCHA verification; responds with "invalid-input-response" :( //string recaptchaResponse = this.Request.Form["__RequestVerificationToken"]; //var client = _clientFactory.CreateClient(); //try //{ // var parameters = new Dictionary<string, string> //{ // {"secret", _configuration["reCAPTCHA:SecretKey"]}, // {"response", recaptchaResponse}, // {"remoteip", this.HttpContext.Connection.RemoteIpAddress.ToString()} //}; // HttpResponseMessage response = await client.PostAsync("https://www.google.com/recaptcha/api/siteverify", new FormUrlEncodedContent(parameters)); // //HttpResponseMessage response = await client.PostAsync("https://www.google.com/recaptcha/api/siteverify" + "?secret=" + parameters["secret"] + "&response=" + parameters["response"], new StringContent("", Encoding.UTF8, "application/x-www-form-urlencoded")); // response.EnsureSuccessStatusCode(); // string apiResponse = await response.Content.ReadAsStringAsync(); // dynamic apiJson = JObject.Parse(apiResponse); // if (apiJson.success != true) // { // this.ModelState.AddModelError(string.Empty, "There was an unexpected problem processing this request. Please try again."); // } //} //catch (HttpRequestException ex) //{ // // Something went wrong with the API. Let the request through. // _logger.LogError(ex, "Unexpected error calling reCAPTCHA api."); //} if (!ModelState.IsValid) { _logger.LogError("Comment post error"); return(RedirectToPage()); } NewComment.UserId = int.Parse(User.Identity.GetUserId()); NewComment.PostId = Post.PostId; NewComment.CommentDate = DateTime.Now; //NewComment.CommentContent = NewComment.CommentContent.Replace(Environment.NewLine, "<br/>"); _context.Comments.Add(NewComment); await _context.SaveChangesAsync(); return(RedirectToPage()); }
public async Task <IActionResult> OnPostAsync() { if (!(User.Identity.GetUserId() == Post.UserId.ToString() || User.IsInRole("admin"))) { return(RedirectToPage("/Errors/Unauthorized")); } if (!ModelState.IsValid) { return(Page()); } Post.DateEdited = (DateTime?)DateTime.Now; //Post.PostContent = Post.PostContent.Replace(Environment.NewLine, "<br/>"); _context.Attach(Post).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostExists(Post.PostId)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(Post).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostExists(Post.PostId)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Posts.Add(Post); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } Post.UserId = int.Parse(User.Identity.GetUserId()); Post.DatePosted = DateTime.Now; //Post.PostContent = Post.PostContent.Replace(Environment.NewLine, "<br/>"); _context.Posts.Add(Post); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Post = await _context.Posts.FindAsync(id); if (Post != null) { _context.Posts.Remove(Post); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (!(User.Identity.GetUserId() == Post.UserId.ToString() || User.IsInRole("admin"))) { return(RedirectToPage("/Errors/Unauthorized")); } if (id == null) { return(NotFound()); } Post = await _context.Posts.FindAsync(id); if (Post != null) { _context.Posts.Remove(Post); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <List <string> > CreateUserAsync(BlogUser user, string password) { var errors = new List <string>(); if (user.Email.Length == 0 || user.Email.Length > 50) { errors.Add("Email length is invalid"); } if (user.Login.Length == 0 || user.Login.Length > 50) { errors.Add("Login length is invalid"); } var sameEmail = await _context.BlogUsers.FirstOrDefaultAsync(x => x.Email == user.Email); if (sameEmail != null) { errors.Add("User with this email already exists"); } var sameLogin = await _context.BlogUsers.FirstOrDefaultAsync(x => x.Login == user.Login); if (sameLogin != null) { errors.Add("User with this login already exists"); } if (errors.Count == 0) { var hash = BCrypt.Net.BCrypt.HashPassword(password); BlogUser newuser = new BlogUser { Email = user.Email, Login = user.Login, Password = hash }; _context.BlogUsers.Add(newuser); var result = await _context.SaveChangesAsync(); } return(errors); }