Exemplo n.º 1
0
        public ActionResult ResetPassword(RegisteredUserVM currentUser,
                                          string passwordToken, string userID)
        {
            CaptchaHelper captchaHelper   = new CaptchaHelper();
            string        captchaResponse = captchaHelper.CheckRecaptcha();

            ViewBag.CaptchaResponse = captchaResponse;

            if (captchaResponse == "Valid")
            {
                var userStore = new UserStore <IdentityUser>();
                UserManager <IdentityUser> manager = new UserManager <IdentityUser>(userStore);
                var user = manager.FindById(userID);
                CreateTokenProvider(manager, PASSWORD_RESET);

                if (currentUser.Password == currentUser.ConfirmPassword)
                {
                    IdentityResult result = manager.ResetPassword(userID, passwordToken, currentUser.Password);
                    if (result.Succeeded)
                    {
                        ViewBag.Result = "The password has been reset.";
                    }
                    else
                    {
                        ViewBag.Result = "Failed, password has to be at least 6 characters!";
                    }
                }
            }
            else
            {
                ViewBag.Result = "The password has not been reset.";
            }
            return(View());
        }
Exemplo n.º 2
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());
        }
        bool CaptchaCheck()
        {
            string captchaResponse;

            if (ModelState.IsValid)
            {
                CaptchaHelper captchaHelper = new CaptchaHelper();
                captchaResponse = captchaHelper.CheckRecaptcha();
            }
            else
            {
                captchaResponse = "fail";
            }

            if (captchaResponse != null && captchaResponse.Equals("Valid"))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        public ActionResult Register(RegisteredUserVM newUser)
        {
            var userStore = new UserStore <IdentityUser>();
            UserManager <IdentityUser> manager = new UserManager <IdentityUser>(userStore)
            {
                UserLockoutEnabledByDefault          = true,
                DefaultAccountLockoutTimeSpan        = new TimeSpan(0, 10, 0),
                MaxFailedAccessAttemptsBeforeLockout = 5
            };
            var identityUser = new IdentityUser()
            {
                UserName = newUser.UserName,
                Email    = newUser.Email
            };

            if (ModelState.IsValid)
            {
                CaptchaHelper     captchaHelper   = new CaptchaHelper();
                OneListCAEntities context         = new OneListCAEntities();
                string            captchaResponse = captchaHelper.CheckRecaptcha();
                if (captchaResponse == "Valid")
                {
                    if (manager.FindByEmail(newUser.Email) == null)
                    {
                        ViewBag.CaptchaResponse = captchaResponse;
                        IdentityResult result = manager.Create(identityUser, newUser.Password);
                        if (result.Succeeded)
                        {
                            AspNetUser user = context.AspNetUsers
                                              .Where(u => u.UserName == newUser.UserName).FirstOrDefault();
                            AspNetRole role = new AspNetRole();
                            role.Id   = "User";
                            role.Name = "User";

                            user.AspNetRoles.Add(context.AspNetRoles.Find(role.Id));
                            context.SaveChanges();
                            //add information of user and password to table users in core
                            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);

                            //string email = "Please confirm your account by clicking this link: <a href=\""
                            //                + callbackUrl + "\">Confirm Registration</a>";
                            SendGrid.sendEmail(newUser, callbackUrl);
                            ViewBag.Result = "Please check your email to activate your account!";
                        }
                        else
                        {
                            ViewBag.Result = "User already exist!";
                        }
                    }
                    else
                    {
                        ViewBag.Result = "User already exist!";
                    }
                }
                else
                {
                    ViewBag.Result = "Registration failed!";
                }
            }

            return(View());
        }
Exemplo n.º 5
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());
        }