public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByNameAsync(model.Email);

                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

                var         callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                var         emailFrom   = WebConfigurationManager.AppSettings["emailfrom"];
                MailMessage mailMessage = new MailMessage(emailFrom, model.Email)
                {
                    Subject    = "Reset Password",
                    Body       = "<p><span style=\"font-family: arial;\">Reset your password by clicking <a href=\"" + callbackUrl + "\">here</a></span></p>.",
                    IsBodyHtml = true
                };

                var service = new EmailConfirm();
                await service.SendAsync(mailMessage);

                return(RedirectToAction("ForgotPasswordConfirmation", "Account"));
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName    = model.Email,
                    DisplayName = model.DisplayName,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    AvatarUrl   = WebConfigurationManager.AppSettings["DefaultAvatar"]
                };

                if (UploadHelper.IsWebFriendlyImage(model.Avatar))
                {
                    var fileName = Path.GetFileName(model.Avatar.FileName);
                    model.Avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), fileName));
                    user.AvatarUrl = "/Avatars/" + fileName;
                }

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

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

                    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["emailfrom"];
                    MailMessage mailMessage = new MailMessage(emailFrom, model.Email)
                    {
                        Subject    = "Confirm your account",
                        Body       = "<p><span style=\"font-family: arial;\">Thank you for registering.</span></p><p><span style=\"font-family: arial;\">Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.</span></p>",
                        IsBodyHtml = true
                    };

                    var service = new EmailConfirm();
                    await service.SendAsync(mailMessage);

                    return(RedirectToAction("EmailConfirmationSent", "Account"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Пример #3
0
        public async Task <ActionResult> SendHouseInvitation([Bind(Include = "UserId,Body,Created,SentBy,RecipientEmail")] Invitation invite, string messageBody, string recipient)
        {
            var    time          = DateTimeOffset.Now;
            var    useBy         = time.AddDays(5);
            var    currentUserId = User.Identity.GetUserId();
            var    currentUser   = db.Users.Find(currentUserId);
            var    from          = currentUser.Email;
            var    code          = invite.Code;
            string cb            = Url.Action("AcceptAndRegister", "Account", new { id = invite.HouseholdId, code = code }, protocol: HttpContext.Request.Url.Scheme);

            if (ModelState.IsValid)
            {
                invite.Subject        = $"Invitation From {currentUser.FirstName}";
                invite.SentBy         = currentUser.Email;
                invite.RecipientEmail = recipient;
                invite.TTL            = useBy;
                invite.Code           = Guid.NewGuid();
                invite.HouseholdId    = currentUser.MyHouse.Id;
                invite.IsValid        = true;
                invite.Created        = time;
                invite.Body           = $"{messageBody} <hr /> To join {currentUser.FirstName}'s household, <a href='{cb}' target='_blank'>click here</a>!";
                db.Invitations.Add(invite);

                db.SaveChanges();
            }

            MailMessage mailMessage = new MailMessage(from, invite.RecipientEmail)
            {
                IsBodyHtml = true,
                Subject    = invite.Subject,
                Body       = invite.Body
            };
            var service = new EmailConfirm();
            await service.SendAsync(mailMessage);

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