Example #1
0
        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);
        }
Example #2
0
        // GET api/image/5
        public HttpResponseMessage Get(string id)
        {
            var gridFS = new GridFSRepository();
            var fileId = new ObjectId(id);

            var response = new HttpResponseMessage();

            try
            {
                var fileStream = gridFS.GetFile(fileId);

                response.Content = new StreamContent(fileStream);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
                response.StatusCode = HttpStatusCode.OK;
            }
            catch
            {
                response.StatusCode = HttpStatusCode.NotFound;
            }

            return response;
        }
Example #3
0
        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();
        }