如何在c#中登录基于浏览器的用户?

时间:2015-06-18 08:08:56

标签: c# sql-server

我使用简单的asp文本框和按钮设计了登录页面。 所以现在我可以在浏览器上登录并限制用户是否未登录。 所以问题是如果我复制主页链接并粘贴另一个浏览器然后它去那个页面,因为必须去登录页面所以如何防止这个 实际上我想要一个要求,如果用户登录一个浏览器然后登录并且同一个用户尝试登录另一个浏览器然后登录该应用程序并且应该注销以前的浏览器的应用程序。 下面是我的代码

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();
            }
        }

0 个答案:

没有答案