1. Open the code behind of CreateUserWizard control located under ~/Sitefinity/UserControls/Login folder
2. Subscribe for SendingMail event.
protected override void OnInit(EventArgs e) |
{ |
base.OnInit(e); |
this.CreateUserWizard1.CreatedUser += new EventHandler(CreateUserWizard1_CreatedUser); |
//SUBSCRIBE FOR SENDINGMAIL EVENT |
this.CreateUserWizard1.SendingMail += new MailMessageEventHandler(CreateUserWizard1_SendingMail); |
} |
3. Use MailMessageEventArgs to get/set the email subject and message.
void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e) |
{ |
e.Message.IsBodyHtml = false; |
e.Message.Subject = Subject + Request.ServerVariables["SERVER_NAME"]; |
string subscriptionUrl = string.Concat(SubscriptionUrl, QueryString, CreateUserWizard1.UserName); |
e.Message.Body = MessageBody + subscriptionUrl; |
} |
4. You should implement custom public properties that will allow you to change the subject and body through the control designer instead of hardcoding them.
[System.ComponentModel.Category("Mail attributes")] |
public string Subject |
{ |
get |
{ |
if (string.IsNullOrEmpty(_subject)) |
return "You have requested subscription for:"; |
return _subject; |
} |
set { this._subject = value;} |
} |
[System.ComponentModel.Category("Mail attributes")] |
public string QueryString |
{ |
get |
{ |
if (string.IsNullOrEmpty(_querystring)) |
return "?username="; |
return _querystring; |
} |
set { this._querystring = value; } |
} |
[System.ComponentModel.Category("Mail attributes")] |
public string SubscriptionUrl |
{ |
get |
{ |
if (string.IsNullOrEmpty(_subscriptionUrl)) |
return "http://localhost:2889/EmptyProject/confirmsubscription.aspx"; |
return _subscriptionUrl; |
} |
set { this._subscriptionUrl = value; } |
} |
[System.ComponentModel.Category("Mail attributes")] |
public string MessageBody |
{ |
get |
{ |
if (string.IsNullOrEmpty(_messageBody)) |
return "Please confirm your account using the link:"; |
return _messageBody; |
} |
set { this._messageBody = value; } |
} |
[System.ComponentModel.Category("Mail attributes")] |
public string MailDefinitionFrom |
{ |
get |
{ |
string maildefinition = CreateUserWizard1.MailDefinition.From; |
if (string.IsNullOrEmpty(maildefinition)) |
return string.Empty; |
return CreateUserWizard1.MailDefinition.From; |
} |
set { CreateUserWizard1.MailDefinition.From = value; } |
} |
private string _subject; |
private string _querystring; |
private string _subscriptionUrl; |
private string _messageBody; |
Now I have to create a user control that will check whether the user is valid or not. If the user is valid i will approve the user using MembershipUser.IsApproved property. The user control is simple. In its template I added a Literal control that will be displayed only in case the user does not exist in the database.
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="EnableUser.ascx.cs" Inherits="CustomControls_EnableUser" %> |
<asp:Label ID="Label1" runat="server" /> |
In the code behind I added my logic that would check the query string and passed the value from the key.
using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Web; |
using System.Web.UI; |
using System.Web.UI.WebControls; |
using System.Web.Security; |
public partial class CustomControls_EnableUser : System.Web.UI.UserControl |
{ |
protected void Page_Load(object sender, EventArgs e) |
{ |
Label1.Visible = false; |
// GET THE QUERY STRING KEYS |
string key = Request.QueryString.AllKeys[0]; |
string query = Request.QueryString[0]; |
if (key.Equals(Key) && !String.IsNullOrEmpty(query)) |
{ |
//GET THE USER FROM THE PROVIDER |
MembershipUser user = Membership.GetUser(query); |
if (user != null) |
{ |
user.IsApproved = true; |
Membership.UpdateUser(user); |
Response.Redirect("~/Sitefinity/Login.aspx"); |
} |
else |
{ |
Label1.Visible = true; |
Label1.Text = LabelText; |
} |
} |
} |
[System.ComponentModel.Category("Mail attributes")] |
public string Key |
{ |
get |
{ |
if(string.IsNullOrEmpty(_key)) |
return "username"; |
return _key; |
} |
set |
{ |
this._key = value; |
} |
} |
[System.ComponentModel.Category("Mail attributes")] |
public string LabelText |
{ |
get |
{ |
if (string.IsNullOrEmpty(_labelText)) |
return "There is no such user registered"; |
return _labelText; |
} |
set |
{ |
this._labelText = value; |
} |
} |
private string _key; |
private string _labelText; |
} |
Note that the value of QueryString property of CreateUserWizard control should be the same as the value of Key property in EnableUser control.