none
把加密函数写在类里面时不能登录了 RRS feed

  • 问题

  • BBS类里面:{
         // string str = ConfigurationManager.ConnectionStrings["sqlconnectionstrings"].ConnectionString;
            SqlConnection conn = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand("verifyUser",conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.NVarChar, 20);
            cmd.Parameters.Add("@password", SqlDbType.NVarChar, 20);
            cmd.Parameters.Add("@Return_Value", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmd.Parameters["@username"].Value = strUName;
            cmd.Parameters["@password"].Value = Encrypt(strUPWD);
            int iRet;
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                iRet = Convert.ToInt32(cmd.Parameters["@Return_Value"].Value);
            }
            catch (SqlException ex) { throw new Exception(ex.Message, ex); }
            finally { conn.Close(); }
          
            return iRet;

        }
        private  string Encrypt(string UPWD)
        {
            byte[] pass = Encoding.UTF8.GetBytes(UPWD);
            MD5 md5 = new MD5CryptoServiceProvider();
           return Encoding.UTF8.GetString(md5.ComputeHash(pass));
        }
    登录事件: protected void Button1_Click(object sender, EventArgs e)
        {
            MYBBS BBS = new MYBBS();                                                                                                                                                                                                                                                                                                                               
            string strUName = TextBox1.Text.ToString();
            string strUPWD = TextBox2.Text.ToString();     
            int iRet = BBS.Login(strUName, strUPWD);   
          
         switch (iRet)
          {
              case 0: Response.Redirect("BBS.aspx"); break;
              case 1: Label1.Text = "用户名或密码错误"; break;
              default: break;
          }     
           
          
        }第一次时把加密函数写到登录时间里面了 ,可以登录的 。后来我想可能以后还得用到这个函数,就写到类里面了,
    可是每次登录都不能成功,不知道到底是哪里出问题了,哪位大哥能帮帮我。还可以给我我的代码提出各种批评

    2009年12月16日 14:24

答案

全部回复