In this tutorial, we will see a better approach towards the registration and login process in an ASP.NET, using C#, where will see hash passwords and how we manage the password correctly in login form. Also, we will see how to authenticate the duplicate user during the registration process.

Prerequisites

Initial Chamber

Step 1

Open Your Visual Studio 2015 and create an empty Website. Give a suitable name [RegForm_demo].

Step 2

In Solution Explorer, you get your empty Website, followed by adding three Web Forms and SQL Server database.

For Web Form

RegForm_demo (Your Empty Website) -> Right click -> Add New Item -> Web Form. Name it as Register.aspx. Now, go to the same process, add other Web form and name it --> login.aspx and home.aspx.

For SQL Server database

RegForm_demo (Your Empty Website) -> Right click -> Add New Item -> SQL Server database. Add the database inside the App_Data_folder.

Database Chamber

Step 3

In Server Explorer, click on your database [Database.mdf] - -> Tables - -> Add New Table - -> Make the table, as shown below.

ASP.NET

Store Procedure

sp_insert

  1. CREATE PROCEDURE [dbo].[sp_insert]
  2. (
  3. -- Add the parameters for the stored procedure here
  4. @Username varchar(50),
  5. @Password varchar(50),
  6. @Email varchar(50)
  7. )
  8. as
  9. Begin
  10. Declare @Count int
  11. Declare @codereturn int
  12. Select @Count = COUNT(Username)
  13. from tbl_data where Username = @Username
  14. If @Count > 0
  15. Begin
  16. Set @codereturn = -1
  17. End
  18. Else
  19. Begin
  20. Set @codereturn = 1
  21. Insert into tbl_data values(@Username,@Password,@Email)
  22. End
  23. Select @codereturn as ReturnValue
  24. End

sp_select

  1. CREATE PROCEDURE sp_select
  2. @Username varchar(50),
  3. @Password varchar(50)
  4. AS
  5. BEGIN
  6. Declare @Count int
  7. Select @Count = COUNT(Username)
  8. from tbl_data where [Username] =@Username and [Password] =@Password
  9. If (@Count = 1)
  10. Begin
  11. Select 1 as codereturn
  12. End
  13. Else
  14. Begin
  15. Select -1 as codereturn
  16. End
  17. END

Design code

Step 4

Now, make some design for your Application by going to Register.aspx and design it, as shown below.

Register.aspx

ASP.NET

Login.aspx

ASP.NET

Code Chamber

Step 5

We will make some code in Register.aspx.cs page, so that our Register form works.

Register.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. using System.Data;
  9. using System.Data.Sql;
  10. using System.Web.Security;
  11. namespace WebApplication3
  12. {
  13. public partial class Register : System.Web.UI.Page
  14. {
  15. protected void Button1_Click(object sender, EventArgs e)
  16. {
  17. SqlConnection con = new SqlConnection(@"Data Source=Nilesh;Initial Catalog=test_db;Integrated Security=True");
  18. SqlCommand cmd = new SqlCommand("sp_insert", con);
  19. cmd.CommandType = CommandType.StoredProcedure;
  20. string encryp = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "SHA1");
  21. cmd.Parameters.AddWithValue("@Username", TextBox1.Text);
  22. cmd.Parameters.AddWithValue("@Password", encryp);
  23. cmd.Parameters.AddWithValue("@Email", TextBox4.Text);
  24. con.Open();
  25. int codereturn = (int)cmd.ExecuteScalar();
  26. if (codereturn == -1)
  27. {
  28. lblmsg.Text = "Username already exist!";
  29. lblmsg.ForeColor = System.Drawing.Color.Red;
  30. }
  31. else
  32. {
  33. Response.Redirect("~/Login.aspx");
  34. }
  35. }
  36. }
  37. }

Login.aspx.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Web.Security;
  10. namespace WebApplication3
  11. {
  12. public partial class Login : System.Web.UI.Page
  13. {
  14. protected void Button1_Click(object sender, EventArgs e)
  15. {
  16. if (authenticate(TextBox1.Text, TextBox2.Text))
  17. {
  18. Response.Redirect("~/Home.aspx");
  19. }
  20. else
  21. {
  22. Label1.Text = "Invalid Username and Password";
  23. Label1.ForeColor = System.Drawing.Color.Red;
  24. }
  25. }
  26. private bool authenticate(string Username, string Passsword)
  27. {
  28. SqlConnection con = new SqlConnection(@"Data Source=Nilesh;Initial Catalog=test_db;Integrated Security=True");
  29. SqlCommand cmd = new SqlCommand("sp_select", con);
  30. cmd.CommandType = CommandType.StoredProcedure;
  31. string encryp = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "SHA1");
  32. cmd.Parameters.AddWithValue("@Username", TextBox1.Text);
  33. cmd.Parameters.AddWithValue("@Password",encryp);
  34. con.Open();
  35. int codereturn = (int)cmd.ExecuteScalar();
  36. return codereturn == 1;
  37. }
  38. }
  39. }

Output

The user is registering with the username abc and password abc too, and if successful, the login page will open, else the respective error message will be shown. Also, this data is saved in the database, as shown below. You can see the password is encrypted in hash format.
ASP.NET

ASP.NET

Let’s say, the user abc is registered and now another user comes. Register with the same username as abc and the authenticate procedure will call and an error will be shown, as given below.
ASP.NET

Login page output

After successful registration, the user abc can access his account, using login access.

ASP.NET

If login is successfuk, home page will be opened, else the respective error will be shown.

ASP.NET

Hope, you liked it. Have a good day and thank you for reading.