Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
            {
                //open the db connection if it is closed...  
                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();// for taking single value
                Session["userName"] = userName;
                Session["RegistrationId"] = (command.Parameters["@CompRegId"].Value).ToString();
                //lblLoginMessage.Text = (command.Parameters["@CompRegId"].Value).ToString(); //Convert.ToString(Session["RegistrationId"]);
                string userType = (command.Parameters["@userType"].Value).ToString();
                string versions = (command.Parameters["@versions"].Value).ToString();
                if (usercount == 1)  // comparing users from table 
                {
                    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);
                        //lblDisplay.Text = "<Marquee><h1><font color=red>Already Logged IN</font></h1></marquee>";
                    }
                }
                else
                {
                    lblLoginMessage.Text = "Invalid User Details";  //for invalid login
                    lblLoginMessage.Visible = true;
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Exception Message: " + ex.Message.Replace("'", "").Replace("\"", "") + "');", true);
            }  
                    finally //Close db Connection if it is open....  
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
                command.Dispose();
            }
        }
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jun-15 11:27am    
Which browser has a concept of "login"? I never heard of such things.
—SA

1 solution

The web is stateless, it doesn't know who is currently logged in elsewhere so you can't do this reliably.
 
Share this answer
 
Comments
dawood abbas 18-Jun-15 6:32am    
then how to do it?
F-ES Sitecore 18-Jun-15 6:32am    
You can't.
Sergey Alexandrovich Kryukov 18-Jun-15 11:30am    
Please read my short blog article; it is exactly about inquirers like you:
Unhappy Inquirer or Is the Abuse the Main Purpose of Programming?
Can you see the point?
—SA
dawood abbas 18-Jun-15 8:21am    
I have one demo project which was developed in php in that happening na.
Sergey Alexandrovich Kryukov 18-Jun-15 11:32am    
5ed. I wrote a blog article to describe all such situations, referenced above. :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900