ASP.NET-用户登录相关问题! [英] ASP.NET- USER Login related Problem!

查看:71
本文介绍了ASP.NET-用户登录相关问题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我创建了ASP.NET Web应用程序,当用户登录到系统时,我所做的工作是将用户的登录名存储在静态变量中,并在主题页中显示名称.但是当多用户使用同一应用程序时,它会进入显示不同的名称作为登录名".我猜静态变量已更改为登录系统的最后一位". E:x如果Jhon第一次登录到系统..,则显示为Jhon,然后Watson登录到系统.然后,当Jhon转到另一页时,显示为"Watson".这确实是我在此应用程序中遇到的关键问题.我认为静态变量对于每个用户都将获得不同的asp.server对象.但是它似乎是错误的.因此,任何人都推荐一种解决此问题的方法.\

登录页面中的代码:

如果(Page.IsValid)
           {

               UserValidation UserV = new UserValidation();
               int a = UserV.check_Login(UNtxt.Text,PWtxt.Text,List_company_cmbo.Text);
               如果(a == 1)
               {
                   //Response.Write("<   SCRIPT    语言  ='  JavaScript' > 警报(可以正常登录")<  /脚本 > );
                   //Response.Write("<   a     href   ='  WebForm1.aspx' >  <  /a  > );
                   //WebForm1 wb = new WebForm1();
                   Session ["logged"] ="logged";
                   Response.Redirect("Home.aspx");

               }

               否则(a == 3)
               {

                   //warning_txt.Text =";
                   //warning_txt.Visible = true;
                   System.Web.HttpContext.Current.Response.Write(" SCRIPT  语言  JavaScript  >  alert(操作将被取消.请与系统管理员联系"). span> /SCRIPT  > );

               }


               别的
               {
                   //Response.Redirect("index.aspx");
                   warning_txt.Visible = true;
               }



           }
           别的
           {

               Page_PreRender();

           } 





CS类中的代码:

public static string F_name;
       public static string Comp_Code;
       public static string Comp_sqnumber;
       static SqlCommand cmd;

       public int check_Login(string userName, string Password, string c_id)
       {
           DatabaseTrans dTrans = new DatabaseTrans();
           string page = "UserValidation.cs";
           string d = userName;
           string pwr = Password.Trim();
           string sql;
           int a = 0;
           // string Nname;


           string cipherPW = CryptorEngine.Encrypt(pwr, true);

           try
           {


               sql = "Select * from tblUser where UserID = ''" + userName + "'' and userPassword = ''" + cipherPW + "'' and compID =''" + c_id + "'' ";
               cmd = new SqlCommand(sql, DatabaseTrans.getConn());
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataTable dt = new DataTable();


               da.Fill(dt);

               if (dt.Rows.Count == 1)
               {
                   a = 1;
                   F_name = dt.Rows[0][3].ToString();
                   Comp_Code = dt.Rows[0][1].ToString();
                   Comp_sqnumber = dt.Rows[0][2].ToString();
               }
               else
               {
                   a = 0;
               }

              // DatabaseTrans.closeCon();
               return a;

           }
           catch (Exception ex)
           {
               ErrorLog el = new ErrorLog();
               el.ErrorRegistry(ex, page, "User Validation.cs under login click", "NA", "NA");
               a = 3;
               return a;

           }




有人可以帮我这对我有很大的帮助!谢谢预先!!!!!!!!!!!网络项目-不够确定).

将用户名存储在会话中或cookie中,具体取决于您希望登录的持续时间.会话存储通常会在20分钟后过期,您可以指定cookie的存储时间.


好吧,您回答了自己的问题(几乎是!),这就是静态变量的作用.它们只能包含整个会话的一个值,因此,如果另一个用户登录,则前一个用户的详细信息将被丢弃,并由新用户替换.

这将使您处于良好的状态,首先学习ASP.NET页面生命周期,以了解ASP.NET Web应用程序或页面从头到尾的工作方式.它如何处理页面级别或实例级别的变量.

关于登录问题,请使用自定义表单身份验证,其示例遍布整个Internet.您还可以使用为每个用户创建的Session变量,并根据相应会话的有效性来识别用户.


Hi i created ASP.NET web Application and when users are login to the System what i did is i stored users Login name in a Static variable and Showed name in the theme page.But when multiusers used the same Application its get to show different names as Login Name. i guess static variable was changed to Last one who Login to the System. E:x if Jhon 1st login to system ..it is shown as Jhon and then Watson logged to the system. Then when Jhon go to another page it is shown as ''Watson''. This is really Crutial issue that i faced in this application. i thought that static variables are get different asp.server objects for each and every user.but its seems to be wrong. so does any one recommend a method for this issue.\

Code in Login page :

if (Page.IsValid)
           {

               UserValidation UserV = new UserValidation();
               int a = UserV.check_Login(UNtxt.Text, PWtxt.Text, List_company_cmbo.Text);
               if (a == 1)
               {
                   //Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Login is ok')</SCRIPT>");
                   //Response.Write("<a href='WebForm1.aspx'></a> ");
                   //  WebForm1 wb = new WebForm1();
                   Session["logged"] = "logged";
                   Response.Redirect("Home.aspx");

               }

               else if (a == 3)
               {

                   // warning_txt.Text = "";
                   // warning_txt.Visible = true;
                   System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Action Will be cancelled. Please contact the system administrator')</SCRIPT>");

               }


               else
               {
                   // Response.Redirect("index.aspx");
                   warning_txt.Visible = true;
               }



           }
           else
           {

               Page_PreRender();

           }





Code in CS Class :

public static string F_name;
       public static string Comp_Code;
       public static string Comp_sqnumber;
       static SqlCommand cmd;

       public int check_Login(string userName, string Password, string c_id)
       {
           DatabaseTrans dTrans = new DatabaseTrans();
           string page = "UserValidation.cs";
           string d = userName;
           string pwr = Password.Trim();
           string sql;
           int a = 0;
           // string Nname;


           string cipherPW = CryptorEngine.Encrypt(pwr, true);

           try
           {


               sql = "Select * from tblUser where UserID = ''" + userName + "'' and userPassword = ''" + cipherPW + "'' and compID =''" + c_id + "'' ";
               cmd = new SqlCommand(sql, DatabaseTrans.getConn());
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataTable dt = new DataTable();


               da.Fill(dt);

               if (dt.Rows.Count == 1)
               {
                   a = 1;
                   F_name = dt.Rows[0][3].ToString();
                   Comp_Code = dt.Rows[0][1].ToString();
                   Comp_sqnumber = dt.Rows[0][2].ToString();
               }
               else
               {
                   a = 0;
               }

              // DatabaseTrans.closeCon();
               return a;

           }
           catch (Exception ex)
           {
               ErrorLog el = new ErrorLog();
               el.ErrorRegistry(ex, page, "User Validation.cs under login click", "NA", "NA");
               a = 3;
               return a;

           }




CAN Someone help me on that that would be a great help for me!Thanks in Advance!!!

解决方案

Don''t use static variables (at all in a web project - it''s not deterministic enough).

Store the user name in the Session, or in a cookie, depending on how long you want a log in to last. Session storage expires normally after about 20 minutes, cookie you can specify how long it is stored for.


Well, you answered your own question (almost!), this is what static variables do. They can only contain one value for the entire session, so if another user logs in the details of the previous user would be discarded and replaced by this new one.

It would put you in good stead to learn the ASP.NET page lifecycle first to understand how an ASP.NET web application or page works from start to finish. How does it deal with page level or instance level variables.

On the issue of login, use custom forms authentication the examples of which are all over the internet. You can also use a Session variable created for every user and identify the user based on the validity of the corresponding session.


这篇关于ASP.NET-用户登录相关问题!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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