Email verification with asp.net membership API

The membership framework does not have built-in e-mail verification functionality. Use following steps to activate user account by confirming with an email.
I assume that you have already created your membership database and have a working smtp server that can send emails.

Step 1 : Store user in database as unapproved.

We register the new user as usual and set isApproved value to false. This indicates that the user is on the database but is inactive and cannot log in (until we verify user e-mail).

Membership.CreateUser(txtUserName.Text, txtPassword.Text,txtEmail.Text, “question”,se, out status);

Step 2 : Send Verification Email.

we send an email to the new user with link on how to verify their account.
When the user clicks on the Register button create user account then pass guid to the link as query string parameter.

Button Register OnClick Event :

protected void btnRegister_Click(object sender, ImageClickEventArgs e)
{
//register
MembershipCreateStatus createStatus;
MembershipUser User = Provider.CreateUser(username, password, email, pwQuestion, pwAnswer, false, null, out createStatus);
// The user was created successfully, send the verification e-mail.
Guid guid = (Guid)user.ProviderUserKey;
StringBuilder Message = new StringBuilder();
Message.Append(“Thank you for creating an account with us”);
Message.Append(“Please click the below link to activate your account”);
Message.Append(string.Format(“Activate “, userName, userId.ToString()));
MailMessage email = new MailMessage();
email.From = new MailAddress(“admin@YourDomain.com”);
email.To.Add(new MailAddress(txtEmail.text));
email.Subject = “Email Verification “;
email.Body = Message.ToString();
email.IsBodyHtml = true;

// Send the email
SmtpClient Emailclient = new SmtpClient();
Emailclient .Send(email);
}

Step 3 : Verification of Email link

This is last step and we are ready to go live! Now we should receive the request and verify that the GUID is the right one.

// Activate the User
if (String.IsNullOrEmpty(Request.Params[“Id”]))
{
// We do not have the userId.
lblMsg.Text=”We do not have the userId.”;
}
else
{
try
{
Guid userId = new Guid(Request.Params[“Id”]);
MembershipUser user = Membership.GetUser(userId);
// Activate the user
user.IsApproved = true;
// Update the user activation
Membership.UpdateUser(user);
lblMsg.Text = “We successfully activate the user.”;
}
catch
{
// Error. Redirect some where
lblMsg.Text = “something went wrong.”;
}
}

Here, we get the UserName and his guid as QueryString parameters and use membership api to activate the user.
And that’s it, your are now all set to activate users via Email Message.

Do please send your comments . I would be happy to hear from you.
Thank you , Happy Coding !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s