Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

How to configure GenerateEmailConfirmationToken in web forms.

$
0
0

I am trying to get an email sent when a user is created in Asp.Net Identity that allows them to confirm an email address. The message "No IUserTokenProvider is registered" continues to pop-up. This is how I have it configured. Note: Can you assist in getting the app to send an email with the token?

This code is in a custom web form page that creates a new user and GenerateEmailConfirmationToken

protected void addUser_btn_Click(object sender, EventArgs e)
    {

        string uname = username_txt.Text;
        string pwd = password_txt.Text;
        string email = email_txt.Text;

        var manager = new UserManager();
        var user = new ApplicationUser() { UserName = uname };

            IdentityResult result = manager.Create(user, pwd);
            if (result.Succeeded)
            {
                manager.SetEmail(user.Id, email);
                manager.AddToRole(user.Id, "CouncilAdmin");

                var code = manager.GenerateEmailConfirmationToken(user.Id);
                var callbackUrl = "/confirmEmail.aspx";

                manager.SendEmail(user.Id,
                    "Confirm your account","Please confirm your account by clicking this link: <a href=\""+ callbackUrl + "\">link</a>");
            }
            else
            {
                addUserResult_lbl.Text = result.Errors.FirstOrDefault();
            }
    }

In IdentityModel.cs I have setup the EmailService and included the dataProtectionProvider within ApplicaitonUserManager

        public class ApplicationUserManager : UserManager<ApplicationUser>
        {
            public ApplicationUserManager(IUserStore<ApplicationUser> store)
                : base(store)
            {
            }

            public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
            {
                var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
                // Configure validation logic for usernames
                manager.UserValidator = new UserValidator<ApplicationUser>(manager)
                {
                    AllowOnlyAlphanumericUserNames = false,
                    RequireUniqueEmail = true
                };

                // Configure validation logic for passwords
                manager.PasswordValidator = new PasswordValidator
                {
                    RequiredLength = 6,
                    RequireNonLetterOrDigit = false,
                    RequireDigit = false,
                    RequireLowercase = false,
                    RequireUppercase = false,
                };

                manager.EmailService = new EmailService();

                var dataProtectionProvider = options.DataProtectionProvider;
                if (dataProtectionProvider != null)
                {
                    manager.UserTokenProvider =
                        new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
                }
                return manager;
            }
        }

        public class EmailService : IIdentityMessageService
        {

            public Task SendAsync(IdentityMessage message)
               {
                   string text = string.Format("Please click on this link to {0}: {1}", message.Subject, message.Body);
                   string html = "Please confirm your account by clicking this link: <a href=\"" + message.Body + "\">link</a><br/>";

                   html += HttpUtility.HtmlEncode(@"Or click on the copy the following link on the browser:" + message.Body);

                   //Create the msg object to be sent
                   MailMessage msg = new MailMessage();
                   msg.From = new MailAddress("webmaster@domain.com");
                   msg.To.Add(new MailAddress(message.Destination));
                   msg.Subject = message.Subject;
                   msg.IsBodyHtml = true;
                   msg.Body = html;

                   //Configure an SmtpClient to send the mail.
                   SmtpClient client = new SmtpClient("host", 25);
                   client.EnableSsl = false; //only enable this if your provider requires it
                   //Setup credentials to login to our sender email address ("UserName", "Password")
                   NetworkCredential credentials = new NetworkCredential("username", "password");
                   client.Credentials = credentials;

                   //Send the msg
                   client.Send(msg);

                   return client.SendMailAsync(msg);

               }

        }

Any help would be greatly appreciated. 

Thanks
Brad


Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>