using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
public partial class main : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "简易论坛—用户登录";
    }
    protected void ButtonEnter_Click(object sender, EventArgs e)
    {
        if (TextBoxName.Text == "" || TextBoxPassword.Text == "")
        {
            Response.Write("<script language=javascript>alert('用户名或密码不得为空!');</script>");
            return;
        }
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" + "Data Source=" + Server.MapPath("App_Data/msg.mdb");
        conn.Open();
        string strSecPwd=FormsAuthentication.HashPasswordForStoringInConfigFile(TextBoxPassword.Text,"MD5");
        string strSQL="select ulevel from manager where uname='"+TextBoxName.Text+"'and upwd='"+strSecPwd+ "'";
        OleDbCommand com=new OleDbCommand(strSQL,conn);        OleDbDataReader dr=com.ExecuteReader();
        dr.Read();
        string UserLevel;
        if(dr.HasRows)
            {
              UserLevel=dr["ulevel"].ToString();
              if (UserLevel == "0")
              {
                  Session["pass"] = "admin";
                  Response.Redirect("manager.aspx");
              }
              else
              {
                  Session["pass"] = "guest";
                  Response.Redirect("guest.aspx");
              }
            }
        else
            {
               Response.Write("<script language=javascript>alert('用户名或密码错误');</script>");
               return;
            }
            dr.Close();
            conn.Close();
            }
    protected void ButtonRegister_Click(object sender, EventArgs e)
    {
        Response.Redirect("register.aspx");
    }
}

解决方案 »

  1.   

    怎么又发一贴??你断点跟踪下值看看,因为你使用了MD5,所以感觉你下面第一句获取的strSecPwd值作为第二句SQL的条件,到数据库中查找,是没有符合条件的记录,所以if(dr.HasRows)的循环根本不会被执行到。
    将你的SQL语句,拿到查询分析器中执行下,看有数据没有???
    string strSecPwd=FormsAuthentication.HashPasswordForStoringInConfigFile(TextBoxPassword.Text,"MD5");
      string strSQL="select ulevel from manager where uname='"+TextBoxName.Text+"'and upwd='"+strSecPwd+ "'";