protected void CreateUser_Click(object sender, EventArgs e) { string userName = UserName.Text; var manager = new AuthenticationIdentityManager(new IdentityStore()); User u = new User(userName) { UserName = userName }; IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text); if (result.Success) { manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false); //OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); using (SocialNetworkDbEntities context = new SocialNetworkDbEntities()) { UserDetail details = new UserDetail(); context.UserDetails.Add(details); context.SaveChanges(); var user = context.AspNetUsers.Find(u.Id); user.UserDetailsId = details.UserDetailsId; context.SaveChanges(); } Response.Redirect("~/Account/UserDetails"); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } }
protected void LinkButtonAddComment_Click(object sender, EventArgs e) { using (SocialNetworkDbEntities context = new SocialNetworkDbEntities()) { if (!string.IsNullOrEmpty(this.TextBoxNewComment.Text)) { try { Comment comment = new Comment(); context.Comments.Add(comment); comment.PostId = this.postId; comment.DateCreated = DateTime.Now; comment.UserId = User.Identity.GetUserId(); comment.Text = Server.HtmlEncode(this.TextBoxNewComment.Text); context.SaveChanges(); Response.Redirect("UserPost.aspx?postId=" + comment.PostId, false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage("Comment failed! Server error!"); } } else { ErrorSuccessNotifier.AddErrorMessage("Cannot post empty comment!"); } } }
protected void AcceptFriendRequestLinkButton_OnCommand(object sender, CommandEventArgs e) { try { string friendId = e.CommandArgument.ToString(); SocialNetworkDbEntities contex = new SocialNetworkDbEntities(); using (contex) { var friend = contex.AspNetUsers.FirstOrDefault(u => u.Id == friendId); var currentUserId = User.Identity.GetUserId(); var currentUser = contex.AspNetUsers.FirstOrDefault(u => u.Id == currentUserId); currentUser.Friends.Add(friend); contex.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Friend request accepted!"); } } catch (Exception exception) { ErrorSuccessNotifier.AddErrorMessage(exception); } }
protected void AddFriendLinkButton_OnCommand(object sender, CommandEventArgs e) { try { string userId = Request.Params["userID"]; SocialNetworkDbEntities context = new SocialNetworkDbEntities(); using (context) { var user = context.AspNetUsers.FirstOrDefault(u => u.Id == userId); var currentUserId = User.Identity.GetUserId(); var currentUser = context.AspNetUsers.FirstOrDefault(u => u.Id == currentUserId); currentUser.Friends.Add(user); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Friend request sent!"); this.AddFriendLinkButton.Visible = false; } } catch (Exception exception) { ErrorSuccessNotifier.AddErrorMessage(exception); } }
protected void ButtonUploadImage_Click(object sender, EventArgs e) { if (!this.FileUploadControl.HasFile) { ErrorSuccessNotifier.AddErrorMessage("No image attached."); return; } if(this.FileUploadControl.PostedFile.ContentType == "image/jpeg" || this.FileUploadControl.PostedFile.ContentType == "image/png" || this.FileUploadControl.PostedFile.ContentType == "image/gif") { int length = FileUploadControl.PostedFile.ContentLength; if (length > 819200) // 800Kb = 1024 * 800 { ErrorSuccessNotifier.AddErrorMessage("The lenght of image is too big. Image must be less than 800kb."); return; } using (SocialNetworkDbEntities context = new SocialNetworkDbEntities()) { var user = context.AspNetUsers.FirstOrDefault(usr => usr.Id == this.userId); if (user != null) { byte[] fileData = new byte[length + 1]; Stream fileStream = FileUploadControl.PostedFile.InputStream; fileStream.Read(fileData, 0, length); user.UserDetail.AvatarImage = fileData; context.SaveChanges(); this.profilePicture.Src = "data:image/jpeg;base64," + Convert.ToBase64String(fileData); } } } else { ErrorSuccessNotifier.AddErrorMessage("The file was not in correct format! Only .jpeg, .png and .gif are supported."); } }
protected void LinkButtonVote_Click(object sender, EventArgs e) { SocialNetworkDbEntities context = new SocialNetworkDbEntities(); using (context) { var post = context.Posts.Find(this.postId); if (post != null && !(post.UsersLiked.Any(usr => usr.Id == User.Identity.GetUserId()))) { var currentUser = context.AspNetUsers.Find(User.Identity.GetUserId()); if (currentUser != null) { try { post.Votes++; post.UsersLiked.Add(currentUser); context.SaveChanges(); FillPageContent(); this.LinkButtonVote.Visible = false; } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage("Voting failed! Server error!"); } } else { ErrorSuccessNotifier.AddErrorMessage("User account error! Contact administrator..."); } } else { ErrorSuccessNotifier.AddErrorMessage("Error occured while registering your vote!" + Environment.NewLine + "Either post doesn't exist or you have already gave your vote"); } } }
protected void ButtonSubmit_Click(object sender, EventArgs e) { if (this.CheckIsValid("Details")) { using (SocialNetworkDbEntities context = new SocialNetworkDbEntities()) { var user = context.AspNetUsers.Find(userId); var userDetails = user.UserDetail; userDetails.City = TrimWords(TextBoxCity.Text); userDetails.Company = TrimWords(TextBoxCompany.Text); userDetails.Email = TrimWords(TextBoxEmail.Text); if (!string.IsNullOrEmpty(TextBoxBirhtDate.Text)) { userDetails.BirthDate = DateTime.Parse(TextBoxBirhtDate.Text); } context.SaveChanges(); } ErrorSuccessNotifier.AddSuccessMessage("Details Succsessfuly changed"); } }
protected void BannUserLinkButton_OnClick(object sender, EventArgs e) { string userToBannId = Request.Params["userID"]; SocialNetworkDbEntities context = new SocialNetworkDbEntities(); try { using (context) { var userToBann = context.AspNetUsers.FirstOrDefault(u => u.Id == userToBannId); if (userToBann == null) { throw new ArgumentException("User does not exist!"); } var role = context.AspNetRoles.FirstOrDefault(r => r.Name == "banned"); userToBann.AspNetRoles.Clear(); userToBann.AspNetRoles.Add(role); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage(string.Format("User \"{0}\" banned!", userToBann.UserName)); } } catch (Exception exception) { ErrorSuccessNotifier.AddErrorMessage(exception.Message); } }
protected void Post_Command(object sender, CommandEventArgs e) { using (SocialNetworkDbEntities context = new SocialNetworkDbEntities()) { try { Post post = new Post(); context.Posts.Add(post); post.AuthorId = User.Identity.GetUserId(); post.DateCreated = DateTime.Now; post.Text = Server.HtmlEncode(this.TextBoxNewPostText.Text); context.SaveChanges(); Response.Redirect("UserPost.aspx?postId=" + post.PostId, false); } catch (Exception ex) { ErrorSuccessNotifier.AddErrorMessage("Posting failed! Server error!"); } } }