代码之家  ›  专栏  ›  技术社区  ›  Zied.M

身份验证用户错误(管理员或简单用户)

  •  0
  • Zied.M  · 技术社区  · 6 年前

    我有一个身份验证界面,用户必须输入登录名和密码才能连接。 将显示的界面根据连接的登录配置文件(用户或管理员)而定

    用户表包含(user、passwd、profile、mail)

    这是我的密码

    在authentication.cs中:

    public Boolean profile_user (string log)
    {
       string value = "";
       SqlCommand cmd = conn.CreateCommand ();
       cmd.CommandText = "select profile from user where user = '" + log + "';";
       SqlDataReader s = cmd.ExecuteReader ();
       if (s.Read ())
       {value = s ["profile"]. ToString (); }
       if (value == "admin")
       {return true; }
       else {return false; }
    }
    

    在authentication.aspx.cs中

    login = new profile_user (name.Text)
    if (login == true)
    {Response.Redirect ("admin.aspx"); }
    else
    {Response.Redirect ("user_c.aspx"); }
    

    错误总是,它执行其他部分,也就是说无论输入的登录名是什么,总是出现用户的窗口

    完整的类(authentication.cs)是:

    using System.Data.SqlClient;
    using System;
    using System.Data;
    using System.Windows.Forms;
    namespace my_pfe
    {  
    SqlConnection conn = new database().connect_utilisateur();
     // database is another class 
    public void class new_user(string nom,string pass, string mail , string profile) 
     {insert into instruction}
    public Boolean search_user(string login)
     { select requete }
    public Boolean profile_user (string log)
    {
    string value = "";
    SqlCommand cmd = conn.CreateCommand ();
    cmd.CommandText = "select profile from user where user = '" + log + "';";
    SqlDataReader s = cmd.ExecuteReader ();
    if (s.Read ())
    {value = s ["profile"]. ToString (); }
    if (value == "admin")
    {return true; }
    else {return false; }
    }
    

    我在value=s[“profile”].tostring()中设置了一个断点,value的值是“admin”,但admin有很多空间(value和valeur:同名)

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  1
  •   Héctor M.    6 年前

    编辑:必须检查数据库中的值,以确保它们不包含空格,如果不包含空格,则可以使用 String.Trim() 移除字符串的所有空白的方法。

    尝试:

    在authentication.cs中:

    using System.Data.SqlClient;
    using System;
    using System.Data;
    using System.Windows.Forms;
    
    namespace MyPfe
    {
       public class NewUser
       {
           private SqlConnection conn = null;
    
           public NewUser()
           {
               this.conn = new database().connect_utilisateur();
           }
    
           public void AddUser(string nom,string pass, string mail , string profile)
           {
               //Insert into
           }
    
           public bool SearchUser(string login)
           {
               //Select requete
           }
    
           public bool ProfileUser(string log)
           {
               string value = string.Empty;
               var cmd = this.conn.CreateCommand();
               cmd.CommandText = string.Format("select profile from user where user = '{0}'", log);
               var s = cmd.ExecuteReader();
               if (s.Read()) value = s["profile"].ToString().Trim();
               return value == "admin";
           }
       }
    }
    

    在authentication.aspx.cs中:

    var NewUser = new MyPfe.NewUser();
    if(NewUser.ProfileUser(name.Text)) Response.Redirect ("admin.aspx");
    else Response.Redirect ("user_c.aspx");
    
        2
  •  0
  •   Kiran Sripada    6 年前

    我认为在从asp.net执行db调用时,不应该在查询中保留“;”。所以试着移除它并检查它是否有效另外,为什么不设置一个断点并检查传入的值

    s ["profile"]. ToString ()