Ejemplo n.º 1
0
        public static async Task ConfirmationEmail(string email, UrlHelper url)
        {
            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(userContext));
            var userASP     = userManager.FindByEmail(email);

            if (userASP == null)
            {
                return;
            }

            try
            {
                var machineKeyProtectionProvider = new MachineKeyProtectionProvider();
                userManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(machineKeyProtectionProvider.Create("EmailConfirmation"));

                string code = await userManager.GenerateEmailConfirmationTokenAsync(userASP.Id);

                code = HttpUtility.UrlEncode(code);

                string callbackUrl = url.Link("DefaultApi", new { controller = "Users/ConfirmEmail", userId = userASP.Id, code });

                var mailType      = WebConfigurationManager.AppSettings["ConfirmEmailType"].ToString().ToLower();
                var emailTemplate = db.MailTemplates.Where(m => m.Type.ToLower().Equals(mailType)).ToList();

                var subject = emailTemplate[0].Subject;
                var body    = emailTemplate[0].Body.Replace("{##}", callbackUrl);

                await MailHelper.SendMail(email, subject, body);
            }
            catch (Exception e) {
                UsersController.errorMessage = e.Message;
                return;
            }
        }
Ejemplo n.º 2
0
        public static async Task <bool> PasswordRecovery(string email)
        {
            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(userContext));
            var userASP     = userManager.FindByEmail(email);

            if (userASP == null)
            {
                return(false);
            }

            var random      = new Random();
            var newPassword = Membership.GeneratePassword(8, 3);
            var machineKeyProtectionProvider = new MachineKeyProtectionProvider();

            userManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(machineKeyProtectionProvider.Create("PasswordRecover"));

            var token = await userManager.GeneratePasswordResetTokenAsync(userASP.Id);

            var response = await userManager.ResetPasswordAsync(userASP.Id, token, newPassword);

            if (response.Succeeded)
            {
                var mailType      = WebConfigurationManager.AppSettings["ResetPasswordEmailType"].ToString().ToLower();
                var emailTemplate = db.MailTemplates.Where(m => m.Type.ToLower().Equals(mailType)).ToList();

                var subject = emailTemplate[0].Subject;
                var body    = emailTemplate[0].Body.Replace("{##}", newPassword);

                await MailHelper.SendMail(email, subject, body);
            }

            return(true);
        }
Ejemplo n.º 3
0
        public static async Task <bool> ConfirmEmail(string userId, string code)
        {
            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(userContext));
            var machineKeyProtectionProvider = new MachineKeyProtectionProvider();

            userManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(machineKeyProtectionProvider.Create("EmailConfirmation"));

            var result = await userManager.ConfirmEmailAsync(userId, code);

            return(result.Succeeded);
        }
Ejemplo n.º 4
0
        public static async Task <bool> ChangePassword(string userId, string password)
        {
            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(userContext));

            var machineKeyProtectionProvider = new MachineKeyProtectionProvider();

            userManager.UserTokenProvider = new DataProtectorTokenProvider <ApplicationUser>(machineKeyProtectionProvider.Create("ResetPassword"));

            var token = await userManager.GeneratePasswordResetTokenAsync(userId);

            var response = await userManager.ResetPasswordAsync(userId, token, password);

            return(response.Succeeded);
        }