Introduction

In this blog, I will demonstrate how to create user registration form with an encrypted password using ASP.NET step by step and save the encrypted password in a database table.

Step 1

Open SQL Server 2014 and create a database table UserRegistration.

  1. CREATE TABLE [dbo].[UserRegistration](
  2. [ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [nvarchar](50) NULL,
  4. [Email] [nvarchar](50) NULL,
  5. [PhoneNumber] [nvarchar](50) NULL,
  6. [Password] [nvarchar](50) NULL,
  7. [Created] [datetime] NULL,
  8. CONSTRAINT [PK_UserRegistration] PRIMARY KEY CLUSTERED
  9. (
  10. [ID] ASC
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  12. ) ON [PRIMARY]
  13. GO
  14. CREATE procedure [dbo].[spRegister]
  15. (
  16. @Name nvarchar(50),
  17. @Email nvarchar(50),
  18. @PhoneNumber nvarchar(50),
  19. @Password nvarchar(50),
  20. @Created datetime
  21. )
  22. as
  23. begin
  24. insert into [dbo].[UserRegistration](Name,Email,PhoneNumber,Password,Created)
  25. values(@Name,@Email,@PhoneNumber,@Password,GETDATE())
  26. end
ASP.NET

Step 2

Open Visual Studio 2015 to create an empty web application project and give it a meaningful name. Right-click or double-click on web config file and check for database connection in it.

  1. <connectionStrings>
  2. <add name="DBCS" connectionString="data source=DESKTOP-M021QJH\SQLEXPRESS; database=SampleDB; integrated security=true;"/>
  3. </connectionStrings>

Add the below line of code if you get a validation error.

  1. <appSettings>
  2. <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  3. </appSettings>

Step 3

Right-click on the project, select "Add", choose web form, and name it RegisterForm.

Add script and bootstrap 4 style plugin files.

  1. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  2. <link href="Content/bootstrap.min.css" rel="stylesheet" />
  3. <script src="scripts/jquery-3.3.1.min.js"></script>
  4. <script src="scripts/bootstrap.min.js"></script>
  5. <style>
  6. .bottom {
  7. margin-bottom: 5px !important;
  8. }
  9. </style>

Design the web form using textbox control, button control, and validation control. Then, apply respective bootstrap 4 classes.

  1. <body>
  2. <form id="form1" runat="server">
  3. <div class="container py-4">
  4. <div class="col-md-5 offset-md-3">
  5. <div class="card card-outline-secondary rounded-0">
  6. <div class="card-header bg-success rounded-0">
  7. <h4 class="text-center text-uppercase text-white">Registration</h4>
  8. </div>
  9. <div class="card-body">
  10. <div class="form-group bottom">
  11. <label>Name</label>
  12. <div class="input-group">
  13. <div class="input-group-prepend">
  14. <div class="input-group-text"><i class="fa fa-user"></i></div>
  15. </div>
  16. <asp:TextBox ID="txtName" runat="server" CssClass="form-control"></asp:TextBox>
  17. </div>
  18. <asp:RequiredFieldValidator ID="rfvName" ControlToValidate="txtName" CssClass="text-danger" runat="server" ErrorMessage="Please enter name"></asp:RequiredFieldValidator>
  19. </div>
  20. <div class="form-group bottom">
  21. <label>Email</label>
  22. <div class="input-group">
  23. <div class="input-group-prepend">
  24. <div class="input-group-text"><i class="fa fa-envelope"></i></div>
  25. </div>
  26. <asp:TextBox ID="txtEmail" runat="server" CssClass="form-control"></asp:TextBox>
  27. </div>
  28. <asp:RequiredFieldValidator ID="rfvEmail" Display="Dynamic" ControlToValidate="txtEmail" CssClass="text-danger" runat="server" ErrorMessage="Please enter email address"></asp:RequiredFieldValidator>
  29. <asp:RegularExpressionValidator ID="revEmail" ControlToValidate="txtEmail" CssClass="text-danger" runat="server" ErrorMessage="Enter valid email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
  30. </div>
  31. <div class="form-group bottom">
  32. <label>Phone Number</label>
  33. <div class="input-group">
  34. <div class="input-group-prepend">
  35. <div class="input-group-text"><i class="fa fa-phone"></i></div>
  36. </div>
  37. <asp:TextBox ID="txtPhoneNumber" runat="server" CssClass="form-control"></asp:TextBox>
  38. </div>
  39. <asp:RequiredFieldValidator ID="rfvPhoneNumber" Display="Dynamic" ControlToValidate="txtPhoneNumber" CssClass="text-danger" runat="server" ErrorMessage="Please enter phone number"></asp:RequiredFieldValidator>
  40. <asp:RegularExpressionValidator ID="revPhoneNumber" ControlToValidate="txtPhoneNumber" CssClass="text-danger" runat="server" ErrorMessage="Enter valid phone number" ValidationExpression="[0-9]{10}"></asp:RegularExpressionValidator>
  41. </div>
  42. <div class="form-group bottom">
  43. <label>Password</label>
  44. <div class="input-group">
  45. <div class="input-group-prepend">
  46. <div class="input-group-text"><i class="fa fa-lock"></i></div>
  47. </div>
  48. <asp:TextBox ID="txtPassword" TextMode="Password" runat="server" CssClass="form-control"></asp:TextBox>
  49. </div>
  50. <asp:RequiredFieldValidator ID="rfvPassword" ControlToValidate="txtPassword" CssClass="text-danger" runat="server" ErrorMessage="Please enter password"></asp:RequiredFieldValidator>
  51. </div>
  52. <div class="form-group bottom">
  53. <label>Confirm Password</label>
  54. <div class="input-group">
  55. <div class="input-group-prepend">
  56. <div class="input-group-text"><i class="fa fa-lock"></i></div>
  57. </div>
  58. <asp:TextBox ID="txtConfirmPassword" TextMode="Password" runat="server" CssClass="form-control"></asp:TextBox>
  59. </div>
  60. <asp:CompareValidator ID="CompareValidator1" ControlToCompare="txtPassword" Display="Dynamic" ControlToValidate="txtConfirmPassword" CssClass="text-danger" runat="server" ErrorMessage="Password does not match"></asp:CompareValidator>
  61. <asp:RequiredFieldValidator ID="rfvConfirmPassword" ControlToValidate="txtConfirmPassword" CssClass="text-danger" runat="server" ErrorMessage="Please enter password"></asp:RequiredFieldValidator>
  62. </div>
  63. <div class="form-group">
  64. <asp:Button ID="btnRegiter" CssClass="btn btn-success rounded-0 btn-block" runat="server" Text="Register" OnClick="btnRegiter_Click" />
  65. </div>
  66. <asp:Label ID="lblMessage" runat="server"></asp:Label>
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71. </form>
  72. </body>

Step 4

Double-click on the "Register" button and write the following C# code.

Add the following namespace.

  1. using System.Configuration;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Security.Cryptography;
  5. using System.IO;
  6. using System.Text;

Complete code

  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Security.Cryptography;
  6. using System.IO;
  7. using System.Text;
  8. namespace UserRegistration_Demo
  9. {
  10. public partial class RegisterForm : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if (!IsPostBack)
  15. {
  16. ClearTexbox();
  17. }
  18. }
  19. private void ClearTexbox()
  20. {
  21. txtName.Text = string.Empty;
  22. txtEmail.Text = string.Empty;
  23. txtPhoneNumber.Text = string.Empty;
  24. }
  25. private string Encrypt(string clearText)
  26. {
  27. string EncryptionKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
  28. byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
  29. using (Aes encryptor = Aes.Create())
  30. {
  31. Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
  32. encryptor.Key = pdb.GetBytes(32);
  33. encryptor.IV = pdb.GetBytes(16);
  34. using (MemoryStream ms = new MemoryStream())
  35. {
  36. using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
  37. {
  38. cs.Write(clearBytes, 0, clearBytes.Length);
  39. cs.Close();
  40. }
  41. clearText = Convert.ToBase64String(ms.ToArray());
  42. }
  43. }
  44. return clearText;
  45. }
  46. protected void btnRegiter_Click(object sender, EventArgs e)
  47. {
  48. try
  49. {
  50. string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
  51. using (SqlConnection con = new SqlConnection(CS))
  52. {
  53. SqlCommand cmd = new SqlCommand("spRegister", con);
  54. cmd.CommandType = CommandType.StoredProcedure;
  55. con.Open();
  56. cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
  57. cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
  58. cmd.Parameters.AddWithValue("@PhoneNumber",txtPhoneNumber.Text.Trim());
  59. cmd.Parameters.AddWithValue("@Password", Encrypt(txtPassword.Text.Trim()));
  60. cmd.Parameters.AddWithValue("@Created", DateTime.Now);
  61. cmd.ExecuteNonQuery();
  62. ClearTexbox();
  63. lblMessage.Text = "You have registered succussfully";
  64. lblMessage.ForeColor= System.Drawing.Color.Green;
  65. }
  66. }
  67. catch (Exception)
  68. {
  69. lblMessage.Text = "You have not registered";
  70. lblMessage.ForeColor = System.Drawing.Color.Red;
  71. }
  72. }
  73. }
  74. }

Step 5 - Run the project by pressing "Ctrl+F5".

Screenshot 1

ASP.NET

Screenshot 2
ASP.NET

Screenshot 3

ASP.NET