public ActionResult Edit(string newUserName, string userToBlock, string userToUnblock, HttpPostedFileBase avatar) { var oldUserName = HttpContext.User.Identity.Name; var userManager = new UserManager(); var user = userManager.GetUserByUserName(oldUserName); Boolean changed = false; if (newUserName != null && newUserName != "" && oldUserName != newUserName && !userManager.IsRegistered(newUserName)) { // Change the user name and client information // Drop the old cookie and create a new one FormsAuthentication.SignOut(); FormsAuthentication.SetAuthCookie(newUserName, false); user.Username = newUserName; changed = true; } if (userToBlock != null && userToBlock != userToUnblock) { if (userToBlock != oldUserName && userToBlock != newUserName && userManager.IsRegistered(userToBlock)) { user.BlockedUsers.Add(userToBlock); changed = true; } if (user.BlockedUsers.Contains(userToUnblock)) { user.BlockedUsers.Remove(userToUnblock); changed = true; } } if (avatar != null) { var gridFs = new GridFSRepository(); gridFs.RemoveFile(user.AvatarId); user.AvatarId = gridFs.UploadFile(avatar.InputStream, avatar.FileName); changed = true; } if (changed == true) { userManager.SaveChanges(user); } return View(user); }
public ActionResult Register(Models.User user, HttpPostedFileBase avatar) { var userManager = new UserManager(); var gridFS = new GridFSRepository(); if (ModelState.IsValid) { //check if user already exists if (!userManager.IsRegistered(user.Username)) { user.AvatarId = gridFS.UploadFile(avatar.InputStream, avatar.FileName); userManager.RegisterUser(user); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", "The supplied username is already taken."); } } return View(); }