Twitter Feed Popout byInfofru

Send Welcome email to user using Membership API

While using Membership API, It is a very normal task to send user a registration or verification email. We can achieve that using two ways one is using the default email setting of Create User Control and other is by implementing CreateUserWizard.CreatedUser event

Method 1:

Drag and Drop the create user control, right click then properties and find MailDefination and set appropriate values for each property.

membership_sc1

BodyFileName is basically the location of the file which contains the body of the email. It can be text file or html file.

HTML file which I am using is a very simple html file which can be found in the project source specified at the end of this post.

Create User Control uses the SMTP settings in web.config to send the email that is why before checking it you need to make sure that you have specify the correct settings in configuration file. For example

   1: <system.net>
   2:     <mailSettings>
   3:         <smtp deliveryMethod="Network">
   4:             <network defaultCredentials="true" host="localhost" port="25"/>
   5:             <!--userName="" password="" if required specify after port-->
   6:         </smtp>
   7:     </mailSettings>
   8: </system.net>

That's it your control is now ready to send emails.

Method 2:

In this method, we will manually shoot an email when the user get registered successfully. For that we need to implement our logic in CreatedUser event of this control.

Following is the screen shot of different events provided by CreateUserControl

image

As you can see I have generated a method against CreatedUser and here is the code for that

   1: Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles CreateUserWizard1.CreatedUser
   2:     Dim ToAddress As String = CreateUserWizard1.Email
   3:     Dim mm As New MailMessage("Support@mysite.com", ToAddress)
   4:     Using objSr As New StreamReader(Server.MapPath("MailTemplate\\welcome_ver.htm"), FileMode.Open)
   5:         With mm
   6:             .Subject = "Welcome to my site"
   7:             .Body = objSr.ReadToEnd()
   8:             .Body = .Body.Replace("{verification_code}", New Random().Next()) 'Any Random number you can put your logic here...
   9:             .IsBodyHtml = True
  10:         End With
  11:         Dim smtp As New SmtpClient
  12:  
  13:         smtp.Send(mm)
  14:     End Using
  15: End Sub

In this method I am also using the SMTP Settings specified in web.config however, you can change that according to your own need.

In line no 10, I have put a token which will replace the string which I have in my source html file with a random number. For those who are facing difficulty in understanding the token stuff, please see the following html

   1: <html>
   2:  
   3: <body>
   4: Hi, 
   5:  
   6: Thank you for the registration and welcome to my site
   7: Your verification code : {verification_code}
   8: </body>
   9: </html>

That is very simple and very easy. I personally recommend the Method 2 as it can give you some extra control over the process.

Here is the VS 2008 code

Configuration Membership API Asp.net 2.0

Membership API is yet another enhancement from asp.net team. In this post, I will guide you through the configuring of Membership API to the enhanced usage.

So, to get started let me clear one more confusion which most of people have in there mind

They think that Membership API does not use any database behind and asp.net runtime engine perform some miracle at the back end  and all the user information is stored somewhere which is known by runtime engine.

 

That is wrong ..... big WRONG ....

By default, asp.net uses SQL Server as the back end of Membership API. If you have planned to use it, before doing any thing you need to implement the schema of Membership API to your database.

Step 1 (Implement Schema & Configure Database):

To do that, you need to use aspnet_regsql. which can be accessed by the instruction give below.

  • Go to X:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727 (version of dotnet framework can be vary but it should be at least 2.0)
  • Find and execute aspnet_regsql which will appear following screen.

aspnet_regsql_screen1

  • Once you click next, you will be asked to "Configure SQL Server for application services" or "Remove application services information from existing database". You need to select the first one.
  • Select the appropriate database, click next and you are done with the database stuff.

Step 2 (Configure Asp.net Application):

In this step we will configure our existing or new asp.net application to user asp.net membership API.

Please bear in mind that Asp.net Web Application Administration is a very handy and useful tool to check Membership API integration.

Now, to use API you need to specify the connection string of the database which we have configured with aspnet_regsql.

  • Add the following connection string in web.config
   1: <add name="ConnectionString" connectionString="Data Source=SAMHEAVEN;Initial Catalog=Northwind;User Id=sa;Password=usam;"
   2:      providerName="System.Data.SqlClient" />
  • Specify membership and role configuration under system.web of web.config.
   1: <roleManager enabled="true">
   2:    <providers>
   3:      <clear/>
   4:      <add name="AspnetSqlRoleProvider" applicationName="/AppName" connectionStringName="ConnectionString" type="System.Web.Security.SqlRoleProvider"/>
   5:    </providers>
   6:  </roleManager>
   7:  <membership>
   8:    <providers>
   9:      <clear/>
  10:      <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ConnectionString" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="/DanyTech" requiresUniqueEmail="true" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
  11:    </providers>
  12:  </membership>

 

  • Finally, you need to set authentication type.
   1: <authentication mode="Forms" />

To be descriptive, we have specified settings for two basic parts of Membership API. which is using Connection String called "connectionString" which we have created in the first step.

AspNetSqlMembershipProvider and  AspnetSqlRoleProvider have an attribute called applicationName which is really important. All the information of the Membership API will stored in the database against the applicationName specified here.

Where as, AspNetSqlMembershipProvider have a list of settings. Click here to get the complete list of Membership Provider settings.

Now the configuration stuff is complete. You can check is every thing running fine by clicking on the Asp.net Configuration Icon give right above the solution explorer.

se_config

Go to the security tab and check you can create some users and roles (at least administrator, user) . If not then make sure you have not skipped any step.