Esempio n. 1
0
        public ActionResult ChangeUserProfile([Bind(Include = "Id,FirstName,LastName,AvatarPath,Email")] UserProfileViewModel profile, HttpPostedFileBase AvatarPath)
        {
            if (ModelState.IsValid)
            {
                var currentUser = db.Users.Find(profile.Id);
                //Avatar Validator
                //profile picture setting
                if (ImageUploader.IsWebFriendlyImage(AvatarPath))
                {
                    var fileName = Path.GetFileName(AvatarPath.FileName);
                    var ext      = Path.GetExtension(AvatarPath.FileName);
                    //extended this to format the image file with an always unique title
                    var unique        = $"{fileName}-{DateTime.Now}";
                    var slug          = SlugHelper.CreateSlug(unique);
                    var formattedFile = $"{slug}{ext}";
                    AvatarPath.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), formattedFile));
                    //save formatted version to the register viewmodel
                    currentUser.AvatarPath = "/Avatars/" + formattedFile;
                }
                else
                {
                    currentUser.AvatarPath = profile.AvatarPath;
                }

                currentUser.FirstName = profile.FirstName;
                currentUser.LastName  = profile.LastName;
                currentUser.Email     = profile.Email;
                db.SaveChanges();
                return(RedirectToAction("Dashboard", "Home"));
            }
            else
            {
                return(RedirectToAction("ChangesNotSaved", "Home"));
            }
        }
Esempio n. 2
0
        public async Task <ActionResult> Register(RegisterViewModel model, HttpPostedFileBase AvatarPath)
        {
            if (ModelState.IsValid)
            {
                //default avatar picture
                model.AvatarPath = "/Avatars/avatar-placeholder.png";
                //profile picture setting
                if (ImageUploader.IsWebFriendlyImage(AvatarPath))
                {
                    var fileName = Path.GetFileName(AvatarPath.FileName);
                    var ext      = Path.GetExtension(AvatarPath.FileName);
                    //extended this to format the image file with an always unique title
                    var unique        = $"{fileName}-{DateTime.Now}";
                    var slug          = SlugHelper.CreateSlug(unique);
                    var formattedFile = $"{slug}{ext}";
                    AvatarPath.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), formattedFile));
                    //save formatted version to the register viewmodel
                    model.AvatarPath = "/Avatars/" + formattedFile;
                }

                var user = new ApplicationUser {
                    UserName    = model.Email,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    DisplayName = model.DisplayName,
                    AvatarPath  = model.AvatarPath
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

                    var emailFrom = WebConfigurationManager.AppSettings["emailto"];
                    var email     = new MailMessage(emailFrom, model.Email)
                    {
                        Subject    = "Confirm your account",
                        Body       = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>",
                        IsBodyHtml = true
                    };

                    var emailServe = new PersonalEmail();

                    await emailServe.SendAsync(email);


                    return(RedirectToAction("Dashboard", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }