|
 |
 |
This tutorial demonstrates how to add registration emails to verify new accounts with ASP.NET's CreateUserWizard Control using C#.
Modifying the CreateUserWizard Control
At this point in the tutorial I have created a new ASP.NET Web Site. What we need to do now is load up the Register.aspx page in the Account folder for editing in Design mode. We need to modify some properties within the CreateUserWizard Control named RegisterUser. To do this:
- Right click on the CreateUserWizard Control and select Properties.
- Set the DisableCreatedUser to ‘True’
- Set the LoginCreatedUser to ‘False’
This will ensure that when a user account is created it is not approved and the user is not logged in.
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
Configuring SMTP Settings
What I have done for this is setup a new Gmail account which I will be configuring here, but it is important to understand that the SMTP settings will be different based on your email provider. I chose Gmail because their SMTP settings are readily available on the internet. As you go through this just use the correct setting for whatever email it is you are using.
To configure our SMTP Settings:
- Click the ASP.NET Configuration icon in the Solution Explorer to open up the ASP.NET Website Administration Tool.
- In the ASP.NET Website Administration Tool click the Application tab.
- Under the SMTP Settings header, click the Configure SMTP e-mail settings link.
- Under the Configure SMTP Settings header, configure the following properties.
- Server Name, the SMTP of your email provider, in my case smtp.gmail.com.
- Server Port, the Port of your email provider, in my case 587.
- From, the email you will be using, in my case PWRecoveryTutorial@gmail.com.
- Under Authentication, choose Basic
- Sender's user name, the username we use to login to the email we are using, in my case PWRecoveryTutorial@gmail.com.
- Sender's password, the password we use to login to the email account we are using here.
- Click Save.
- Close the ASP.NET Website Administration Tool.
What this actually does is provide a nice user-friendly view of these options and then stores the changes you make in your Web.Config file. Go ahead and open up your Web.Config file from the Solution Explorer. Here you should see something similar to this:
<mailSettings> <smtp from="PWRecoveryTutorial@gmail.com"> <network host="smtp.gmail.com" password="xxxx" port="587" userName="PWRecoveryTutorial@gmail.com" /> </smtp> </mailSettings>
|
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
You will notice that the options we configured were added in here. For the email provider I am using, I am required to enable SSL (Secure Sockets Layer) for me to be able to actually send these emails out. To do this I am simply going to add enableSsl="true" in the network tag. So now my mail settings look like:
<mailSettings> <smtp from="PWRecoveryTutorial@gmail.com"> <network host="smtp.gmail.com" password="xxxx" port="587" userName="PWRecoveryTutorial@gmail.com" enableSsl="true" /> </smtp> </mailSettings>
|
Note that yours will appear differently based on your email provider and that you don’t have to enable SSL unless your email provider requires it.
Adding Code to the Register.aspx.cs Code Behind
Next, we need to add the code to actually create the email message and send it to the user when their account is created. Open up the Register.aspx.cs file for editing. At the top of this class we need to add two extra using statments:
using System.Net.Mail; using System.Text;
|
Notice the RegisterUser_CreatedUser event method, this method executes when a new account is created. At the beginning of this event method we need to add the following code:
//Create a new user MembershipUser newUser = Membership.GetUser(RegisterUser.UserName); //Set the user's id Guid newUserId = (Guid)newUser.ProviderUserKey; //Now we need to create an url that will link to a VerifyNewUser page and //accept a query string that is the user's id //setup the base of the url string urlBase = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath; //setup the second half of the url string verifyUrl = "/VerifyNewUser.aspx?ID=" + newUserId.ToString(); //combine to make the final url string fullUrl = urlBase + verifyUrl; //Now we need to setup the mail message that will be sent //create the mailmessage object MailMessage message = new MailMessage(); //set the bodyhtml to true so we can add html to our message message.IsBodyHtml = true; //set who the email is from, this should correspond to the email we setup //in the smtp settings earlier message.From = new MailAddress("PWRecoveryTutorial@gmail.com"); //send this message to the email of the user who just created an account message.To.Add(new MailAddress(RegisterUser.Email)); //set the subject message.Subject = "New User Registration"; //next we need to build the message body, I'm going to use the stringbuilder //to simplify this a bit. StringBuilder sb = new StringBuilder(); //add the 'Welcome username, ' line sb.Append("Welcome " + RegisterUser.UserName + ", <br/><br/>"); sb.Append("Thank you for registering. To activate your new account please "); //create our link html tag which should look like this: //<a href="fullUrlHere"> sb.Append("<a href=\"" + fullUrl + "\">"); //add the Click Here text that will be the link sb.Append("Click Here"); //end the link html tag sb.Append("</a>"); //Set the body of our message message.Body = sb.ToString(); //Send the message SmtpClient client = new SmtpClient(); client.Send(message);
|
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
Create the VerifyNewUser.aspx Page
Next, we need to add the VerifyNewUser page that we are linking our user to in the registration email. To do this:
- Add a new web form to the project named VerifyNewUser.aspx.
- Add a Label to the web form named Label1.
Once our page is setup, we need to add the code in here to actually approve the user's account if they visit this page with their UserId sent via a query string. The first thing we need to do is add an extra using statement at the top of this class:
using System.Web.Security;
|
Next, we are going to add the code that will approve the user's account in the Page_Load event method:
//if the id is in the query string if (!string.IsNullOrEmpty(Request.QueryString["ID"])) { //store the user id Guid userId = new Guid(Request.QueryString["ID"]); //attempt to get the user's information MembershipUser user = Membership.GetUser(userId); //check if the user exists if (user != null) { //check to make sure the user is not approved yet if (!user.IsApproved) { //approve the user user.IsApproved = true; //update the account in the database Membership.UpdateUser(user); //display a success message Label1.Text = "Your account has been approved!"; } } }
|
|
|
|