Ejemplo n.º 1
0
        public ActionResult Register(RegisteredUser newUser)
        {
            // TAKING THE WRONG MODEL AS INPUT???
            var userStore = new UserStore<IdentityUser>();
            UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore)
            {
                UserLockoutEnabledByDefault = true,
                DefaultAccountLockoutTimeSpan = new TimeSpan(0, 10, 0),
                MaxFailedAccessAttemptsBeforeLockout = 3
            };

            var identityUser = new IdentityUser()
            {
                UserName = newUser.UserName,
                Email = newUser.Email
            };

            // this threw an error, but it also worked so what gives???
            IdentityResult result = manager.Create(identityUser, newUser.Password);
            if (result.Succeeded)
            {
                CreateTokenProvider(manager, EMAIL_CONFIRMATION);
                // identityUser.Id use this to create an entry in our accounts table 
                var code = manager.GenerateEmailConfirmationToken(identityUser.Id);
                var callbackUrl = Url.Action("VerifiedEmail", "Accounts",
                                                new { userId = identityUser.Id, code = code },
                                                    protocol: Request.Url.Scheme);

                string email = "Please confirm your account by clicking this link: <a href=\""
                                + callbackUrl + "\">Confirm Registration</a>";


                ViewBag.FakeConfirmation = email;
                UserAccountVMRepo uaRepo = new UserAccountVMRepo();
                uaRepo.CreateAccount(newUser.FirstName, newUser.LastName, identityUser.Id);

                // CREATE WITH CONSUMER ROLE BY DEFAULT
                SecurityEntities context = new SecurityEntities();
                AspNetUser user = context.AspNetUsers
                                 .Where(u => u.UserName == newUser.UserName).FirstOrDefault();
                AspNetRole role = context.AspNetRoles
                                 .Where(r => r.Name == "consumer").FirstOrDefault();

                user.AspNetRoles.Add(role);
                context.SaveChanges();

                MailHelper mailer = new MailHelper();
                string response = mailer.EmailFromArvixe(
                                           new RegisteredUser(newUser.Email, newUser.Subject = "Confirm Email", newUser.Body = email));

                ViewBag.Response = response;
                return View("ConfirmEmail");
            }
            return View();
        }
Ejemplo n.º 2
0
        public ActionResult ForgotPassword(string email, RegisteredUser userRecovery)
        {
            var userStore = new UserStore<IdentityUser>();
            UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
            var user = manager.FindByEmail(email);
            CreateTokenProvider(manager, PASSWORD_RESET);

            var code = manager.GeneratePasswordResetToken(user.Id);
            var callbackUrl = Url.Action("ResetPassword", "Accounts",
                                         new { userId = user.Id, code = code },
                                         protocol: Request.Url.Scheme);
            var body = "Please reset your password by clicking <a href=\""
                                     + callbackUrl + "\">here</a>";

            MailHelper mailer = new MailHelper();
            string response = mailer.EmailFromArvixe(
                                       new RegisteredUser(userRecovery.Email = email, userRecovery.Subject = "Password Recovery Email", userRecovery.Body = body));
            return View("PasswordEmail");
        }