Ejemplo n.º 1
0
        public ActionResult ForgotPassword(string email)
        {
            //ensures valid input
            if (ModelState.IsValid)
            {
                CaptchaHelper captchaHelper = new CaptchaHelper();
                string captchaResponse = captchaHelper.CheckRecaptcha();
                ViewBag.CaptchaResponse = captchaResponse;

                //creates token to be sent to mail helper to allow password reset through email
                var userStore = new UserStore<IdentityUser>();
                UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore);
                var user = manager.FindByEmail(email);
                CreateToken ct = new CreateToken();
                CreateTokenProvider(manager, PASSWORD_RESET);

                var code = manager.GeneratePasswordResetToken(user.Id);
                var callbackUrl = Url.Action("ResetPassword", "Home",
                                             new { userId = user.Id, code = code },
                                             protocol: Request.Url.Scheme);

                //send callbackUrl to email helper
                MailHelper mailer = new MailHelper();
                string message = "Please reset your password by clicking <a href=\""
                                         + callbackUrl + "\">here</a>";
                string subject = "Please reset your password";
                try
                {
                    mailer.EmailFromArvixe(message, user.Email, subject);
                    ViewBag.FakeEmailMessage =
                        "You have been sent an email to finish reseting your password";
                }
                catch (System.Exception ex)
                {
                    ViewBag.FakeEmailMessage = ex.Message;
                }
            }
            return View();
        }
Ejemplo n.º 2
0
        public ActionResult Register(RegisteredUser newUser)
        {
            //when user registers in checks model requirements to ensure valid input
            if (ModelState.IsValid)
            {
                CaptchaHelper captchaHelper = new CaptchaHelper();
                string captchaResponse = captchaHelper.CheckRecaptcha();
                ViewBag.CaptchaResponse = captchaResponse;

                // add user to database, lock account until email confirmation
                var userStore = new UserStore<IdentityUser>();
                UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore)
                {
                    //set account to lock after consecutive failed login attempts
                    UserLockoutEnabledByDefault = true,
                    DefaultAccountLockoutTimeSpan = new TimeSpan(0, 10, 0),
                    MaxFailedAccessAttemptsBeforeLockout = 3
                };

                var identityUser = new IdentityUser()
                {
                    UserName = newUser.UserName,
                    Email = newUser.Email
                };
                IdentityResult result = manager.Create(identityUser, newUser.Password);

                if (result.Succeeded)
                {
                    samUserRegEntities context = new samUserRegEntities();
                    AspNetUser user = context.AspNetUsers
                        .Where(u => u.UserName == newUser.UserName).FirstOrDefault();
                    AspNetRole role = context.AspNetRoles
                        .Where(r => r.Name == "registered").FirstOrDefault();

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

                    //creates token to be passed to mail helper to allow email confirmation
                    CreateToken ct = new CreateToken();
                    CreateTokenProvider(manager, EMAIL_CONFIRMATION);

                    var code = manager.GenerateEmailConfirmationToken(identityUser.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Home",
                                                    new { userId = identityUser.Id, code = code },
                                                        protocol: Request.Url.Scheme);
                    //send callbackURL to email helper
                    MailHelper mailer = new MailHelper();
                    string email = "Please confirm your account by clicking this link: <a href=\""
                                    + callbackUrl + "\">Confirm Registration</a>";
                    string subject = "Please confirm your email";
                    //try
                    //{
                        mailer.EmailFromArvixe(email, identityUser.Email, subject);
                        ViewBag.FakeConfirmation =
                            "An account confirmation has been sent to your email, please confirm before attempting to login";
                    //}
                    //catch (System.Exception ex)
                    //{
                    //    ViewBag.FakeConfirmation = ex.Message;
                    //}

                }
            }
            return View();
        }