Multiple Login Restriction(using asp.net,C#,MySQL)
Introduction:
Here I will explain about how to restrict Multiple Login at the same time in different browsers.
Description:
Database
Create table in MySQL Like this.
insert the value in the column for login .
Login page :
create aspx page for Login.
HTML Markup
create aspx page for Login.
HTML Markup
Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>Login</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table cellpadding="5" cellspacing="8">
<tr>
<td>
User Name
</td>
<td>
:
</td>
<td>
<asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
:
</td>
<td>
<asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click"
/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Namespaces
You will need to
import the following namespaces.
C#
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
Code Behind
The below event handler gets called when the Submit button is clicked. Here the username and the password is verified . Then the Update query is setting for the current session as flag using id and the username. Flag is for at the same time once user login in one browser, then the same user open the same login and enter in other browsers. First Browsers Session will expired.
Login.aspx.cs
public partial class login : System.Web.UI.Page
{
MySqlConnection
con = new MySqlConnection("server=localhost;database=multi;uid=root;password=****;");
string
query;
MySqlCommand
cmd;
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected
void btnsubmit_Click(object
sender, EventArgs e)
{
con.Open();
query = "select
* from log where name='" + txtusername.Text + "' and password='" + txtpassword.Text + "'";
cmd = new
MySqlCommand(query, con);
// dr =
cmd.ExecuteReader();
MySqlDataAdapter
da = new MySqlDataAdapter(cmd);
DataTable
dt = new DataTable();
da.Fill(dt);
if
(dt.Rows.Count > 0)
{
Session["id"] = Session.SessionID;
Session["txt"] = txtusername.Text;
query = "update log set flag='" + Session["id"].ToString() + "' where name='" + Session["txt"].ToString() + "'";
cmd = new
MySqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("home.aspx");
}
}
}
Home Page
After successful
login user will be redirected to this page.
HTML Markup
HTML Markup
Home.aspx:
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Home.aspx.cs"
Inherits="Home"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"
runat="server">
<title>home</title>
</head>
<body>
<form id="form1" runat="server">
<div align="right">
<table width="100%">
<tr>
<td>
</td>
</tr>
<tr>
<td align="right" >
<asp:Button ID="Button1" runat="server" Text="Logout" onclick="logout_Click" /></td>
</tr>
</table>
</div>
<div>
<br />
<br />
<br />
</div>
<div align="center"
style="font-size: 50px; font-weight: bold; font-style: italic; font-variant: normal; text-transform:
capitalize; color:
#0000FF">
welcome.............
</div>
</form>
</body>
</html>
Namespaces
You will need to
import the following namespaces.
C#
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
Code Behind
At the page load itself Query checks for name and flag .if it not same page redirect to login page otherwise enters. Then Flag is updated for current user.The event handler gets called when the Logout button is clicked. In that Flag updated then Session cleared and redirected to login page.
At the page load itself Query checks for name and flag .if it not same page redirect to login page otherwise enters. Then Flag is updated for current user.The event handler gets called when the Logout button is clicked. In that Flag updated then Session cleared and redirected to login page.
Home.aspx.cs
public partial class Home : System.Web.UI.Page
{
MySqlConnection
con = new MySqlConnection("server=localhost;database=multi;uid=root;password=****;");
string
query;
MySqlCommand
cmd;
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
Method();
Response.Write("welcome," + Session["txt"].ToString());
}
}
protected
void Method()
{
if
(Session["id"] != null)
{
query = "select * from log where name='" + Session["txt"].ToString() + "' and flag='" + Session["id"].ToString() + "'";
cmd = new
MySqlCommand(query, con);
//
MySqlDataReader dr = cmd.ExecuteReader();
MySqlDataAdapter
da = new MySqlDataAdapter(cmd);
DataTable
dt = new DataTable();
da.Fill(dt);
if
(dt.Rows.Count > 0)
{
con.Close();
}
else
{
Response.Redirect("login.aspx");
}
}
else
{
Response.Redirect("login.aspx");
}
}
protected
void Update()
{
con.Open();
query = "update
log set flag='" + Session["id"].ToString()
+ "' where name='" + Session["txt"].ToString() + "'";
cmd = new
MySqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
}
protected
void logout_Click(object
sender, EventArgs e)
{
try
{
con.Open();
query = "update log set flag='" + Session["id"].ToString() + "' where name='" + Session["txt"].ToString() + "'";
cmd = new
MySqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
}
catch
{
throw;
}
finally
{
Session.Clear();
FormsAuthentication.SignOut();
Response.Redirect("login.aspx");
}
}
}
Great article. I'm waiting for ur next article
ReplyDeleteThank you so much. !!!!
ReplyDeleteHi, So that you created the method " protected void Update() " but you use it? Thanks
ReplyDelete