I designed login page by using simple asp textboxes and button. so now i can login on the browser and restrict if user not logged in. so issue is if i copy the home page link and paste it another browser then its going to that page where as have to go to login page so how to prevent this Actually I want a requirement, if user logged in one browser then login and same user trying to logging in another browser then login the application and should logout previous browser's application. below is my code
public void empLogin()
{
try
{
if (connection.State == ConnectionState.Closed)
connection.Open();
string userName = txtUName.Text;
string password = txtPwd.Text;
command = new SqlCommand();
command.CommandText = "sp_Emplogin";
command.CommandType = CommandType.StoredProcedure;
SqlParameter outRegistrationId = command.Parameters.Add("@CompRegId", SqlDbType.Int);
outRegistrationId.Direction = ParameterDirection.Output;
outRegistrationId.Size = 7;
SqlParameter outUserType = command.Parameters.Add("@userType", SqlDbType.VarChar);
outUserType.Direction = ParameterDirection.Output;
outUserType.Size = 7;
SqlParameter outversions = command.Parameters.Add("@versions", SqlDbType.VarChar);
outversions.Direction = ParameterDirection.Output;
outversions.Size = 10;
command.Parameters.AddWithValue("@userName", userName);
command.Parameters.AddWithValue("@password", password);
command.Connection = connection;
int usercount = (Int32)command.ExecuteScalar();
Session["userName"] = userName;
Session["RegistrationId"] = (command.Parameters["@CompRegId"].Value).ToString();
string userType = (command.Parameters["@userType"].Value).ToString();
string versions = (command.Parameters["@versions"].Value).ToString();
if (usercount == 1)
{
if (chkRemember.Checked == true)
{
Response.Cookies["userName"].Value = txtUName.Text;
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
Response.Cookies["password"].Value = txtPwd.Text;
Response.Cookies["password"].Expires = DateTime.Now.AddDays(1);
}
else
{
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(-1);
Response.Cookies["password"].Expires = DateTime.Now.AddDays(-1);
}
StudentInfo _objStudentInfo = new StudentInfo(txtUName.Text, txtPwd.Text);
Session["objStudentInfo"] = _objStudentInfo;
string sKey = txtUName.Text + txtPwd.Text;
string UKey = txtUName.Text + txtPwd.Text;
string UOneKey = txtUName.Text + txtPwd.Text;
string AdminUser = Convert.ToString(Cache["sKey"]);
string User = Convert.ToString(Cache["UKey"]);
string UserOne = Convert.ToString(Cache["UOneKey"]);
if (AdminUser == null || AdminUser == String.Empty)
{
if (userType == "Admin")
{
TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
HttpContext.Current.Cache.Insert("sKey", sKey, null, DateTime.MaxValue, SessTimeOut,
System.Web.Caching.CacheItemPriority.NotRemovable, null);
if (versions == "Version2") {
Response.Redirect("~/AdminWithVersionTwo/AdminDashBoardVTwo.aspx");
}
else if (versions == "Version3")
{
}
else if (versions == "Version1")
{
Response.Redirect("~/Admin/DashBoard.aspx");
}
}
}
if (User == null || User == String.Empty)
{
if (userType == "User")
{
TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
HttpContext.Current.Cache.Insert("UKey", UKey, null, DateTime.MaxValue, SessTimeOut,
System.Web.Caching.CacheItemPriority.NotRemovable, null);
if (versions == "Version2")
{ Response.Redirect("~/UserVTwo/userDashBoardVTwo.aspx");
}
else if (versions == "Version3")
{
}
else if (versions == "Version1")
{
Response.Redirect("~/User/UserDashBoard.aspx");
}
}
}
if (UserOne == null || UserOne == String.Empty)
{
if (userType == "User1")
{
TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
HttpContext.Current.Cache.Insert("UOneKey", UOneKey, null, DateTime.MaxValue, SessTimeOut,
System.Web.Caching.CacheItemPriority.NotRemovable, null);
if (versions == "Version2")
{ Response.Redirect("~/User1VTwo/DashBoardUser1VTwo.aspx");
}
else if (versions == "Version3")
{
}
else if (versions == "Version1")
{ Response.Redirect("~/User1/User1DashBoard.aspx");
}
}
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('This User Alredy logged in');", true);
}
}
else
{
lblLoginMessage.Text = "Invalid User Details";
lblLoginMessage.Visible = true;
}
}
catch (Exception ex)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Exception Message: " + ex.Message.Replace("'", "").Replace("\"", "") + "');", true);
}
finally
{
if (connection.State == ConnectionState.Open)
connection.Close();
command.Dispose();
}
}