Login With Encoded Password | asp.net with c# (Password Encoding par...
Login page .aspx (Design Code) :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoginEnc.aspx.cs" Inherits="test3.password_encrypt.LoginEnc" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1
{
width: 100%;
}
.auto-style2
{
text-align: right;
}
.auto-style3
{
text-align: left;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">
<asp:Label ID="Label1" runat="server" Text="Username : "></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="UsernameTXT" runat="server" Width="230px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label2" runat="server" Text="Password : "></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="PasswordTXT" runat="server" Width="230px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="lblmsg" runat="server"></asp:Label>
</td>
<td class="auto-style3">
<asp:Button ID="LoginBTN" runat="server" Font-Bold="True" Font-Size="Medium" OnClick="LoginBTN_Click" Text="Login" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Login Page .aspx.cs code (Code behind) :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Security.Cryptography;
namespace test3.password_encrypt
{
public partial class LoginEnc : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
static string Encrypt(string value)//for encryption of text
{
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] data = md5.ComputeHash(utf8.GetBytes(value));
return Convert.ToBase64String(data);
}
}
protected void Page_Load(object sender, EventArgs e)
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr1"].ConnectionString;
conn.Open();
}
protected void LoginBTN_Click(object sender, EventArgs e)
{
string passstr = Encrypt(PasswordTXT.Text);//for encrypting the inputed text in password textbox
/*for loging in with comparing the encrypted password*/
SqlDataReader reader;
cmd.Connection = conn;
cmd.CommandText = string.Format("select * from PassEnc where Username='{0}' and Password='{1}'", UsernameTXT.Text, passstr);
reader = cmd.ExecuteReader();
if (reader.Read())
{
Session["Username"] = UsernameTXT.Text;//for creating session
string valueFromSession = Session["Username"].ToString();//for creating session
conn.Close();
Response.Redirect("~/password encrypt/LoginHomePg.aspx");
}
else
{
lblmsg.Text = "Username Or Password Invalid.";
conn.Close();
}
/*-----------------------------------------------------*/
}
}
}
Home Page after Login .aspx (Design Code) :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoginHomePg.aspx.cs" Inherits="test3.password_encrypt.LoginHomePg" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Logout" />
<br />
</div>
<asp:Label ID="lblmsg" runat="server"></asp:Label>
</form>
</body>
</html>
Home Page after Login .aspx.cs code (Code behind) :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace test3.password_encrypt
{
public partial class LoginHomePg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Session["Username"] == null)//if session is not present it will redirect to login page
{
Response.Redirect("~/password encrypt/SaveEnc.aspx");
}
else//if session is present then it will display the welcome mesage
{
lblmsg.Text = "Welcome " + (string)(Session["Username"]) + " you have loged-in successfully";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session.Clear();
Session.Abandon();
Response.Redirect("~/password encrypt/SaveEnc.aspx", true);
}
}
}
Comments
Post a Comment