Name this as Signup.aspx
Legend
Green Text for Comments
Orange Text for Code Snippets
Purple Text for Label Errors and Anti SQL Inject Scripts
Imports System.Data
Imports System.Data.SqlClient
Imports System.Exception
Imports System.Web.Configuration
Imports System.Net.Mail
Imports System.Web
Partial Class _Default
Inherits System.Web.UI.Page
Dim cn As SqlConnection
Dim Com As SqlCommand
Dim dr As SqlDataReader
Dim strCon As String
Dim strQry As String
Dim nowip As String
Dim userID As String
Dim pwd As String
Dim email As String
Dim question As String
Dim answer As String
Dim rand As String = Date.Now.Millisecond * Date.Now.Second * 3 * Year(Now) + Date.Now.Day 'This will generate a random number for activation
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label2.Visible = False
Dim userCleared As New System.Text.StringBuilder 'Variable Used to clear
Dim QuestionCleared As New System.Text.StringBuilder 'Variable Used to clear
Dim AnswerCleared As New System.Text.StringBuilder 'Variable Used to clear
For Each ch As Char In txtUser.Text 'Removes special characters and leaves only a letter or a digit
If Char.IsLetterOrDigit(ch) OrElse ch = " "c Then
userCleared.Append(ch)
End If
Next
For Each ch As Char In txtQuestion.Text 'Removes special characters and leaves only a letter or a digit
If Char.IsLetterOrDigit(ch) OrElse ch = " "c Then
QuestionCleared.Append(ch)
End If
Next
For Each ch As Char In txtAnswer.Text 'Removes special characters and leaves only a letter or a digit
If Char.IsLetterOrDigit(ch) OrElse ch = " "c Then
AnswerCleared.Append(ch)
End If
Next
userID = userCleared.ToString 'Removes special characters and leaves only a letter or a digit
pwd = txtPWD.Text
email = txtEmail.Text
question = QuestionCleared.ToString
answer = AnswerCleared.ToString
If pwd = "" And txtpwd2.Text = "" Then
Label3.Text = "Blank password not accepted"
Label3.Visible = True
Else
If pwd <> txtpwd2.Text Then 'Checks if both passwords are not Equal and Triggers the Label Errors
Label3.Visible = True
Label4.Visible = True
Else
Label3.Visible = False
Label4.Visible = False
If CheckAccountEmail(email) = True Then 'check if email is already used
Response.Write("Email is already used")
Label2.Visible = True
Else
If checkAccount(userID) = False Then'checking of account
createAccount() 'Calls a Method
Response.Write("Account Successfully Created, Please Check your email and activate your account!")
txtUser.Text = ""
txtEmail.Text = ""
txtAnswer.Text = ""
txtQuestion.Text = ""
Else
Label5.Visible = True
txtUser.Text = ""
End If
End If
End If
End If
End Sub
Function checkAccount(ByVal username As String) As Boolean
Try
strCon = System.Configuration.ConfigurationManager.AppSettings("ConnectX").ToString() 'Connects to the Database
strQry = "Select * FROM tUser where sUserID='" & username & "' and sUsername='" & username & "'" 'SQL Query Here
cn = New SqlConnection(strCon)
cn.Open()
Com = New SqlCommand(strQry, cn)
dr = Com.ExecuteReader
If dr.Read < 0 Then 'Tests the Query
Return True
Else
Return False
End If
cn.Close()
dr.Close()
Com.Dispose()
Catch ex As Exception
End Try
End Function
Sub createAccount()
Try
strCon = System.Configuration.ConfigurationManager.AppSettings("ConnectX").ToString()
strQry = "Insert INTO tUser (sUserID,sUserName,sUserPW,Commend,Question,Answer,bIsblock,sUserIP) Values('" & userID & "','" & userID & "','" & pwd & "','" & email & "','" & question & "','" & answer & "','" & True & "','" & nowip & "')" 'Inserting the Account to the Database
cn = New SqlConnection(strCon)
cn.Open()
Com = New SqlCommand(strQry, cn)
Com.ExecuteNonQuery()
Com.Dispose()
cn.Close()
Activation()
Catch ex As Exception
End Try
End Sub
Function CheckAccountEmail(ByVal emailAdd As String) As Boolean
Try
strCon = System.Configuration.ConfigurationManager.AppSettings("ConnectX").ToString()
strQry = "Select * FROM tUser where COMMEND='" & emailAdd & "'" 'Method to check if EMAIL is Existing or Not
cn = New SqlConnection(strCon)
cn.Open()
Com = New SqlCommand(strQry, cn)
dr = Com.ExecuteReader
If dr.Read < 0 Then
Return True
Else
Return False
End If
Catch ex As Exception
End Try
cn.Close()
Com.Dispose()
dr.Close()
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Getting Clients IP
nowip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If nowip = "" Then
nowip = Request.ServerVariables("REMOTE_ADDR")
End If
End Sub
Sub Activation()
Dim activationKey As String = Date.Now.Millisecond * Date.Now.Second * 3 * Year(Now) + Date.Now.Day * 77 'Generates Activation Key
Try
strCon = System.Configuration.ConfigurationManager.AppSettings("ConnectX").ToString()
strQry = "Insert INTO tActivation (sUsername,ActivationCode,ActivationStatus,DateRegistered,DateActivated,AccountEmail) Values('" & userID & "','" & activationKey & "','" & 0 & "','" & Now & "','" & Now & "','" & txtEmail.Text & "')"
cn = New SqlConnection(strCon)
cn.Open()
Com = New SqlCommand(strQry, cn)
Com.ExecuteNonQuery()
Com.Dispose()
cn.Close()
SendToMail(activationKey)
'Calls a Procedure to send the key using SMTP server
Catch ex As Exception
End Try[/color]
End Sub
Sub SendToMail(ByVal activation As String)
Try
Dim mail As New Net.Mail.MailMessage
mail.Subject = "Welcome to Your Company.COM"
mail.To.Add(txtEmail.Text) 'Inserts the email add of the recipient
mail.From = New MailAddress("Support@yourwebsite.com") 'Disregard this one, IDK whats the function of that lol
mail.Body = "Your Activation Code is " & activation & vbCrLf & "To activate your account logon to yourwebsite.com and type in your activation code." & vbCrLf & "This is an automated reply email. Do not reply on this email, Thank you." 'Body of the Message
Dim smtp As New SmtpClient()
smtp.Host = System.Configuration.ConfigurationManager.AppSettings("SMTP") 'Connects to the SMTP Server Config at Web.Config
smtp.EnableSsl = True
smtp.Credentials = New Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings("FROMEMAIL"), System.Configuration.ConfigurationManager.AppSettings("FROMPWD"))
smtp.Port = "587" 'Depends on what port your email server is
smtp.Send(mail)
Catch ex As Exception
Response.Write(ex.Message.ToString)
End Try
End Sub
End Class