Hi nauna,
Please refer below sample.
HTML
Register.aspx
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th colspan="3">
Registration
</th>
</tr>
<tr>
<td>
Username
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ErrorMessage="Required" ForeColor="Red" ControlToValidate="txtUsername"
runat="server" />
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />
</td>
<td>
<asp:RequiredFieldValidator ErrorMessage="Required" ForeColor="Red" ControlToValidate="txtPassword"
runat="server" />
</td>
</tr>
<tr>
<td>
Confirm Password
</td>
<td>
<asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password" />
</td>
<td>
<asp:CompareValidator ErrorMessage="Passwords do not match." ForeColor="Red" ControlToCompare="txtPassword"
ControlToValidate="txtConfirmPassword" runat="server" />
</td>
</tr>
<tr>
<td>
Email
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ErrorMessage="Required" Display="Dynamic" ForeColor="Red"
ControlToValidate="txtEmail" runat="server" />
<asp:RegularExpressionValidator runat="server" Display="Dynamic" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="txtEmail" ForeColor="Red" ErrorMessage="Invalid email address." />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button Text="Register" runat="server" OnClick="RegisterUser" />
</td>
<td>
</td>
</tr>
</table>
Login.aspx
<table>
<tr>
<th colspan="3">
Login
</th>
</tr>
<tr>
<td>
Username
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="Required"
ForeColor="Red" ControlToValidate="txtUsername" runat="server" />
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ErrorMessage="Required"
ForeColor="Red" ControlToValidate="txtPassword" runat="server" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnLogin" Text="Login" runat="server" OnClick="LoginUser" />
</td>
<td>
</td>
</tr>
</table>
Namespaces
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
Code
Register.aspx.cs
protected void RegisterUser(object sender, EventArgs e)
{
string password = Encode(txtPassword.Text);
int userId = 0;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Insert_User"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
cmd.Parameters.AddWithValue("@Password", password);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
cmd.Connection = con;
con.Open();
userId = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
string message = string.Empty;
switch (userId)
{
case -1:
message = "Username already exists.\\nPlease choose a different username.";
break;
case -2:
message = "Supplied email address has already been used.";
break;
default:
message = "Registration successful.\\nUser Id: " + userId.ToString();
Response.Redirect("~/Login.aspx");
break;
}
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
}
}
public static string Encode(string password)
{
byte[] encodeData = new byte[password.Length];
encodeData = Encoding.UTF8.GetBytes(password);
return Convert.ToBase64String(encodeData);
}
Login.aspx.cs
protected void LoginUser(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Password FROM [LoginDB].[dbo].[Users] WHERE Username = @Username", con))
{
cmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
con.Open();
string password = Convert.ToString(cmd.ExecuteScalar());
string decryptPass = Decode(password);
if (decryptPass == txtPassword.Text.Trim())
{
Response.Redirect("~/Home.aspx");
}
con.Close();
}
}
}
public string Decode(string encryptPassword)
{
UTF8Encoding encoder = new UTF8Encoding();
Decoder utf8Decode = encoder.GetDecoder();
byte[] decodeByte = Convert.FromBase64String(encryptPassword);
int count = utf8Decode.GetCharCount(decodeByte, 0, decodeByte.Length);
char[] decodePassword = new char[count];
utf8Decode.GetChars(decodeByte, 0, decodeByte.Length, decodePassword, 0);
return new String(decodePassword);
}
Screenshot