public ActionResult AcceptInvitation(string recipientEmail, string code)
        {
            var realGuid   = Guid.Parse(code);
            var invitation = db.Invitations.FirstOrDefault(i => i.RecipientEmail == recipientEmail && i.Code == realGuid);

            if (invitation == null)
            {
                //Need to create this fail case view - no invitation found
                return(View("NotFoundError", invitation));
            }
            var expirationDate = invitation.Created.AddDays(invitation.TTL);

            if (invitation.IsValid && DateTime.Now < expirationDate)
            {
                var householdName = db.Households.Find(invitation.HouseholdId).HouseholdName;
                ViewBag.Greeting = $"Thank you for accepting my invitation to join the {householdName} House!";
                var model = new AcceptInvitationVM()
                {
                    InvitationId = invitation.Id,
                    Email        = recipientEmail,
                    Code         = realGuid,
                    HouseholdId  = invitation.HouseholdId
                };

                return(View(model));
            }
            return(View("AcceptError", invitation));
        }
        public async Task <ActionResult> AcceptInvitation(AcceptInvitationVM model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, HouseholdId = model.HouseholdId
                };
                if (model.Avatar != null)
                {
                    //Run the avatar upload code from blog/bt here
                }
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    roleHelper.UpdateUserRole(user.Id, "Member");
                    InvitationHelper.MarkAsInvalid(model.InvitationId);
                    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>");

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

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Example #3
0
        public async Task <ActionResult> AcceptInvitation(AcceptInvitationVM model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName    = model.Email,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    HouseholdId = model.HouseholdId
                };

                if (model.Avatar != null)
                {
                    if (FileUploadValidator.IsWebFriendlyImage(model.Avatar))
                    {
                        var fileName = FileStamp.MakeUnique(model.Avatar.FileName);

                        var serverFolder = WebConfigurationManager.AppSettings["DefaultServerFolder"];
                        model.Avatar.SaveAs(Path.Combine(Server.MapPath("~" + serverFolder), fileName));
                        user.AvatarPath = $"{serverFolder}/{fileName}";
                    }
                }

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

                if (result.Succeeded)
                {
                    rolesHelper.AddUserToRole(user.Id, "Member");
                    InvitationHelper.MarkAsInvalid(model.InvitationId);
                    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>");

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

            // If we got this far, something failed, redisplay form

            return(View(model));
        }