Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ole Kristian Losvik 22 posts 73 karma points
    Aug 04, 2016 @ 19:08
    Ole Kristian Losvik
    0

    Verify the email of new member after registration by sending mail with verification code

    I have tried searching this forum to get an updated sample on how to automatically send an email with a verification code to the email provided by the new member upon registration.

    I guess I have to make a surfacecontroller which will make a verification code upon registration, mail it to the user and verify it when the new member clicks on the link? Is there any examples on how this could be done?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Aug 05, 2016 @ 06:33
    Dave Woestenborghs
    0

    Hi Ole,

    I would generate a activation code on registering a member.

    You can store this on the member it self or in a custom database table. I would use custom table because searching through members on a property value can be heavy operation.

    Then you can send that code in a e-mail. When the user uses the code to confirm you can query the database on the code.

    Dave

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Aug 05, 2016 @ 06:38
    Dave Woestenborghs
    1

    Hi Ole,

    This is the code that I use now for a project where we needed to generated unique codes with a variable length. I couldn't use a guid as unique code because this was a database of existing project and it wasn't set up for GUIDs. The code only allows for "hexadecimal" characters but you can extend the allowed number of characters

    public string GenerateUniqueCode(int length)
            {
                char[] chars = "ABCDEF0123456789".ToCharArray();
                byte[] data = new byte[1];
                using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
                {
                    crypto.GetNonZeroBytes(data);
                    data = new byte[length];
                    crypto.GetNonZeroBytes(data);
                }
                StringBuilder result = new StringBuilder(length);
                foreach (byte b in data)
                {
                    result.Append(chars[b % (chars.Length)]);
                }
                return result.ToString();
            }
    

    I ran some test and with a 20 character length code it generated 100.000.000 million unique codes.

    Dave

  • Ole Kristian Losvik 22 posts 73 karma points
    Aug 05, 2016 @ 19:59
    Ole Kristian Losvik
    0

    Thanks for your suggestions, Dave Woestenborghs, however I was hoping somebody had some code samples to provide. For now I have it kind of working with a api controller, but i guess I have to secure it in some way... But I do not how.

    It is not a verification link, however I send the password to the user, so the new member has to have access to the email provided to get access.

    The code for now: ```

         using System;
         using System.Collections.Generic;
         using System.Configuration; 
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    using Umbraco.Web.WebApi;
    using Umbraco.Core;
    using System.Web.Security;
    using System.Net.Mail;
    
    namespace losol.CMS.Controllers
    {
        public class MemberApiController : UmbracoApiController
        {
            private readonly string adminEmail = ConfigurationManager.AppSettings["DefaultFromEmailAddress"];
            private readonly string mailAccount = ConfigurationManager.AppSettings["mailAccount"];
            private readonly string mailPassword = ConfigurationManager.AppSettings["mailPassword"];
    
            // https://domain.com/Umbraco/Api/MemberApi/RegisterMember?name=John+Johnson&[email protected]
            [System.Web.Http.AcceptVerbs("GET", "POST")]
            [System.Web.Http.HttpGet]
            public string RegisterMember(string name, string email)
            {
                // Check if member with email exists
                var member = ApplicationContext.Services.MemberService.GetByEmail(email);
    
                // No member with the email exist, return error message.
                if (member != null)
                {
                    return "Member with email already exist: " + email;
                }
    
                // Create new member
                var newMember = ApplicationContext.Services.MemberService.CreateMember(email, email, name, "Member");
                ApplicationContext.Services.MemberService.Save(newMember);
    
                // Found the member, generate a new password and save it to the member
                var password = Membership.GeneratePassword(8, 2);
                ApplicationContext.Services.MemberService.SavePassword(newMember, password);
    
                // Now send email to the new member with the password
                var emailToSend = new StringBuilder();
                emailToSend.AppendFormat("<p>Welcome to our site. We have generated a password for you. Please use the new password below to login to the site.</p>");
                emailToSend.AppendFormat("<p><b>{0}</b></p>", password);
    
                // Send password in email
                SendEmail(adminEmail, email, "New user", emailToSend.ToString());
    
                // Return a OK status
                return "New member created with email: " + email;
            }
    
    
            // https://domain.com/Umbraco/Api/MemberAPI/[email protected]
            [System.Web.Http.AcceptVerbs("GET", "POST")]
            [System.Web.Http.HttpGet]
            public string ResetPasswordForMember(string email)
            {
                // Lookup member
                var member = ApplicationContext.Services.MemberService.GetByEmail(email);
    
                // No member with the email exist, return error message.
                if (member == null)
                {
                    return "Could not find any member with email: " + email;
                }
    
                // Found the member, generate a new password and save it to the member
                var password = Membership.GeneratePassword(8, 2);
                ApplicationContext.Services.MemberService.SavePassword(member, password);
    
                // Now email the member the password
                var emailToSend = new StringBuilder();
                emailToSend.AppendFormat("<p>We have made a new password for you. Please use the new password below to login to the site.</p>");
                emailToSend.AppendFormat("<p><b>{0}</b></p>", password);
    
                // Send password in email
                SendEmail(adminEmail, email, "New password", emailToSend.ToString());
    
                // Return a OK status
                return "Password is sent to email: " + email;
            }
    
            private string SendEmail(string fromEmail, string toEmail, string subject, string body)
            {
                try
                {
                    MailMessage mailMsg = new MailMessage();
    
                    // Make the email
                    mailMsg.From = new MailAddress(fromEmail);
                    mailMsg.To.Add(new MailAddress(toEmail));
                    mailMsg.Subject = subject;
                    mailMsg.IsBodyHtml = true;
                    mailMsg.Body = body;
    
                    // Init SmtpClient and send
                    SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));
                    System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(mailAccount, mailPassword);
                    smtpClient.Credentials = credentials;
    
                    smtpClient.Send(mailMsg);
                    return "ok";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                    }
                }
            }
        }
    

    ```

Please Sign in or register to post replies

Write your reply to:

Draft