如何在c#中登录基于浏览器的用户? [英] How to login browser based user in c#?

查看:61
本文介绍了如何在c#中登录基于浏览器的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

推荐答案

网络是无状态的,它不知道当前登录的是谁别的,所以你不能可靠地做到这一点。
The web is stateless, it doesn't know who is currently logged in elsewhere so you can't do this reliably.


这篇关于如何在c#中登录基于浏览器的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆