public async Task <IActionResult> OnPostChangeEmailAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } if (!ModelState.IsValid) { await LoadAsync(user); return(Page()); } var email = await _userManager.GetEmailAsync(user); if (Input.NewEmail != email) { var userId = await _userManager.GetUserIdAsync(user); var code = await _userManager.GenerateChangeEmailTokenAsync(user, Input.NewEmail); user.Email = Input.NewEmail; _context.User.Update(user); _context.SaveChanges(); StatusMessage = "Your email has been updated."; return(RedirectToPage()); } StatusMessage = "Your email is unchanged."; return(RedirectToPage()); }
public ActionResult AddComment(Comment comment, int postId) { Comment comm = null; var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var user = _context.User.Include(u => u.Rank).FirstOrDefault(u => u.Id == userId); var post = _context.Post.Include(p => p.Author).Include(p => p.Category).Include(p => p.Approved).FirstOrDefault(p => p.Id == postId); if (comment != null) { comm = new Comment { CommContent = comment.CommContent, DateCreated = comment.DateCreated, User = user, UserId = userId, Post = post, PostId = postId }; if (user != null && post != null) { _context.Add(comm); post.Comments.Add(comm); user.Comments.Add(comm); _context.SaveChanges(); } } return(RedirectToAction("GetComments", "Comments", new { postId = postId })); }
public async Task <IActionResult> OnPostAsync() { ApplicationUser appUser = await _userManager.GetUserAsync(User); if (appUser == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } if (!ModelState.IsValid) { await LoadAsync(appUser); return(Page()); } if (Input.Username != appUser.UserName | Input.FirstName != appUser.FirstName | Input.LastName != appUser.LastName) { var setUsernameResult = await _userManager.SetUserNameAsync(appUser, Input.Username); if (!setUsernameResult.Succeeded) { StatusMessage = "Unexpected error."; return(RedirectToPage()); } appUser.UserName = Input.Username; appUser.FirstName = Input.FirstName; appUser.LastName = Input.LastName; _context.User.Update(appUser); _context.SaveChanges(); } await _signInManager.RefreshSignInAsync(appUser); StatusMessage = "Your profile has been updated"; return(RedirectToPage()); }