以下代码来自chinabs.com一、新建一个数据库
  新建一个access数据user.mdb。
  新建一个user表,添加:UserId(文本类型)及Password(文本类型)两个字段。
二、新建一个default.aspx文件。
  在Web Form里:
  加入两个Label控件,Text属性分别为“登录名”和“密码”;
  加入两个TextBox控件,ID属性分别为“Userid”和“Pwd”,Text属性均为空;
  加入两个RequiredFieldValidato控件,ID属性分别为“rfvUserid”和“rfvPwd”,Text属性分别为“请输入登录名!”和“请输入登录密码!”,ControlToValidate属性分别为"Userid"和"Pwd";
  加入一个Button控件,ID属性为“LogButton”,Text属性别为“登录”;
  最后加入一个Label控件,ID属性为“Msg”。
  Default.aspx源代码如下:
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="lsj.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript (ECMAScript)">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<FONT face="宋体">
<form runat="server" ID="Form1">
<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 82px; POSITION: absolute; TOP: 39px" runat="server" Width="55px" Height="26px">登录名</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 84px" runat="server" Width="63px" Height="24px">密 码</asp:Label>
<asp:TextBox id="Userid" style="Z-INDEX: 103; LEFT: 161px; POSITION: absolute; TOP: 39px" runat="server" Width="109px" Height="25px"></asp:TextBox>
<asp:TextBox id="Pwd" style="Z-INDEX: 104; LEFT: 162px; POSITION: absolute; TOP: 81px" runat="server" Width="109px" Height="22px" TextMode="Password"></asp:TextBox>
<asp:Button id="LogButton" style="Z-INDEX: 105; LEFT: 79px; POSITION: absolute; TOP: 125px" runat="server" Width="59px" Height="25px" Text="登 录"></asp:Button>
<asp:Label id="Msg" style="Z-INDEX: 106; LEFT: 161px; POSITION: absolute; TOP: 130px" runat="server" Width="117px" Height="26px"></asp:Label>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 107; LEFT: 290px; POSITION: absolute; TOP: 43px" runat="server" Width="162px" Height="18px" ErrorMessage="RequiredFieldValidator" ControlToValidate="Userid">请输入登录名!</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator id="RequiredFieldValidator2" style="Z-INDEX: 108; LEFT: 292px; POSITION: absolute; TOP: 83px" runat="server" Width="175px" Height="22px" ErrorMessage="RequiredFieldValidator" ControlToValidate="Pwd">请输入登录密码!</asp:RequiredFieldValidator>
</form>
</FONT>
</body>
</HTML>三、编写default.aspx.cs文件。
  双击LogButton,
  1、加入using System.Data.OleDb;
  2、先在class中声明:
  public string strConnection;
  OleDbConnection myConn;
  3、加入数据库链接:
  把下面代码加入“Page_Init(object sender, EventArgs e)”的“InitializeComponent();”后面.
  string strConnection="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(".")+"..\\user.mdb;";  
  myConn=new OleDbConnection(strConnection);
  4、在LogButton_Click(object sender, System.EventArgs e)事件中加入下面的代码:
string userid,pwd;
userid=Userid.Text;
pwd=Pwd.Text;   
string mySel="SELECT count(*) as iCount from user where UserID=""+userid+""";OleDbCommand myCmd1=new OleDbCommand(mySel,myConn);
myCmd1.Connection.Open();
OleDbDataReader Dr1;
Dr1=myCmd1.ExecuteReader();
Dr1.Read();
string Count=Dr1["iCount"].ToString();
Dr1.Close();
myCmd1.Connection.Close();
string DrPwd,DrRoles;
if(Count!="0")
{
 mySel="SELECT * from user where UserID=""+userid+""";
 OleDbCommand myCmd=new OleDbCommand(mySel,myConn);
 myCmd.Connection.Open();
 OleDbDataReader Dr;
 Dr=myCmd.ExecuteReader();
 Dr.Read();
 DrPwd=Dr["Password"].ToString();
 Dr.Close();
 if(DrPwd==pwd)
 {
  Session["logid"]=userid;
  Response.Redirect("main.aspx");
 }
 else
  Msg.Text="登录密码错.";
}
else
  Msg.Text="没有这个用户.";  好了,全部工作已经完成,default.aspx.cs源代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;namespace lsj
{
 /// <summary>
 /// Summary description for WebForm1.
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.TextBox Userid;
  protected System.Web.UI.WebControls.Button LogButton;  
  protected System.Web.UI.WebControls.TextBox Pwd;
  protected System.Web.UI.WebControls.Label Msg;
  protected System.Web.UI.HtmlControls.HtmlForm Form1;  
  protected System.Web.UI.WebControls.RequiredFieldValidator rfvUserid;
  protected System.Web.UI.WebControls.RequiredFieldValidator rfvPwd;
  public string strConnection;
  OleDbConnection myConn;
 
  public WebForm1()
  {
   Page.Init += new System.EventHandler(Page_Init);
  }  private void Page_Load(object sender, System.EventArgs e)
  { 
  }  private void Page_Init(object sender, EventArgs e)
  {
 InitializeComponent();
 string strConnection="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath(".")+"..\\user.mdb;"; 
 //user.mdb放在与aspx文件同一目录下 
 myConn=new OleDbConnection(strConnection);
  }  private void InitializeComponent()
  {    
 this.LogButton.Click += new System.EventHandler(this.LogButton_Click);
 this.Load += new System.EventHandler(this.Page_Load);
  }
 
  private void LogButton_Click(object sender, System.EventArgs e)
  {    
 string userid,pwd;
 userid=Userid.Text;
 pwd=Pwd.Text;   
 string mySel="SELECT count(*) as iCount from user where UserID=""+userid+""";
    
 OleDbCommand myCmd1=new OleDbCommand(mySel,myConn);
 myCmd1.Connection.Open();
 OleDbDataReader Dr1;
 Dr1=myCmd1.ExecuteReader();
 Dr1.Read();
 string Count=Dr1["iCount"].ToString();
 Dr1.Close();
 myCmd1.Connection.Close();
 string DrPwd,DrRoles;
 if(Count!="0")
 {
  mySel="SELECT * from user where UserID=""+userid+""";
  OleDbCommand myCmd=new OleDbCommand(mySel,myConn);
  myCmd.Connection.Open();
  OleDbDataReader Dr;
  Dr=myCmd.ExecuteReader();
  Dr.Read();
  DrPwd=Dr["Password"].ToString();
  Dr.Close();
  if(DrPwd==pwd)
  {
   Session["logid"]=userid;//新建一个Session
   Response.Redirect("main.aspx");
  }
   else
  Msg.Text="登录密码错.";
 }
 else
   Msg.Text="没有这个用户.";
  }
 }
}

解决方案 »

  1.   

    .net的?给个EMAIL ,我发给你
      

  2.   

    <%@ Page language="c#"debug="true" Codebehind="login.aspx.cs" AutoEventWireup="false" Inherits="_821.WebForm1" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <%@ Import Namespace="System.Data.SqlTypes" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>欢迎您!</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <p><FONT face="宋体"></FONT>&nbsp;</p>
    <p align="center"><font color="#ff0000" size="6"><b>查询系统</b></font></p>
    <p><FONT face="宋体"></FONT>&nbsp;</p>
    <p>&nbsp;</p>
    <form id="Form1" method="post" runat="server">
    <table width="400" border="1" cellspacing="0" cellpadding="3" align="center" bordercolorlight="#003333"
    bordercolordark="#ffffff">
    <tr>
    <td height="25" colspan="2" valign="top" bgcolor="#006699">
    <p align="center"><font color="#ffff66">用户登录</font></p>
    </td>
    </tr>
    <tr>
    <td height="32" valign="top" align="right" style="WIDTH: 112px; HEIGHT: 32px">用户ID
    </td>
    <td height="32" valign="top" style="HEIGHT: 32px">
    <asp:TextBox id="txtMa01" runat="server" />
    </td>
    </tr>
    <tr>
    <td height="32" valign="top" align="right" style="WIDTH: 112px; HEIGHT: 32px">用户密码
    </td>
    <td height="32" valign="top" style="HEIGHT: 32px">
    <asp:TextBox id="txtMa02" textmode="password" runat="server" />
    </td>
    </tr>
    <tr>
    <td height="25" align="center" colspan="2">
    <asp:Button id="butOK" Text="登录" runat="server" OnClick="butOK_Click" BorderStyle="Outset" />
    <asp:Button id="butCancel" Text="取消" runat="server" OnClick="butCancel_Click" BorderStyle="Outset" />
    </td>
    </tr>
    </table>
    <table width="400" border="0" cellspacing="0" cellpadding="3" align="center">
    <tr>
    <td>
    <asp:Label id="lblMsg" runat="server" />
    </td>
    </tr>
    </table>
    </form>
    <script Language="c#" runat="server">
     public void butOK_Click(Object sender, EventArgs E) 
     {
    string ss = "Data source=LYS;Password=abcd1234;Persist Security Info=True;User ID=sa;Initial Catalog=BoloQQ;Data Source=LYS;packet size=4096";
    SqlConnection conn = new SqlConnection(ss);
    conn.Open();
    string sQuery = "select ID  from login where UserID ='"+txtMa01.Text +"'and password='"+txtMa02.Text+"'";
    SqlCommand cmd = new SqlCommand(sQuery,conn);
    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.Read())
    {
    Page.Response.Redirect("main.aspx");
    }
    else
    {
    lblMsg.Text="用户ID或用户密码输入错误!!请重新输入!!";
    txtMa01.Text = "";
    txtMa02.Text = "";
    }

    dr.Close();
    conn.Close();
     }
    public void butCancel_Click(Object sender, EventArgs E)
    {
    txtMa01.Text = "";
    txtMa02.Text = "";
    lblMsg.Text="";
    }
    </script>
    </body>
    </HTML>
      

  3.   

    private SqlConnection SQLConn(string connectstr)
    {
    SqlConnection conn=new SqlConnection(connectstr);
    conn.Open();
    return conn;
    }

    //得到一个SqlDataReader,其中第一个参数是SQL语句,第二个参数是建好的连接
    private SqlDataReader SqlDr(string SQLStr,SqlConnection aConnection)
    {
    SqlCommand mycomm=new SqlCommand(SQLStr,aConnection);
    return mycomm.ExecuteReader();
    }

    //执行一项操作命令
    private void ExcuteComm(string SQLStr,SqlConnection aConnection)
    {
    SqlCommand mycomm=new SqlCommand(SQLStr,aConnection);
    mycomm.ExecuteNonQuery();
    mycomm.Dispose();
    }

    //重置表单
    private void ReSetForm()
    {
    user_i.Text="";
    pass_word.Text="";
    Label_info1.Text ="";
    }

    //显示提示信息
    private void ErrorMessage(string s)
    {
    ReSetForm();
    Label_info1.Text =s;

    }

    //对于经过验证过的用户,把登陆信息写到日志数据库中
    private void WriteLog(string auser_id)
    {
    SqlConnection myconn=SQLConn(ConfigurationSettings.AppSettings["ConnectString"]);
    //写日志
    ExcuteComm("insert into sys_login (user_id,login_time,login_ip) values('"+ auser_id +"',"+"'"+DateTime.Now .ToString ()+"','"+Request.ServerVariables["REMOTE_ADDR"]+"')",myconn);  
    myconn.Close();
    }

    //判断用户名跟密码是否配对
    private bool IsMatch(string auser_id,string apassword)
    {
    SqlConnection myconn=SQLConn(ConfigurationSettings.AppSettings["ConnectString"]);
    SqlDataReader mydr=SqlDr("select * from sys_user where user_id='"+auser_id+"'",myconn);
    string thepassword="";
    if (mydr.Read())
    {
    if (!(mydr.IsDBNull(0)))
    thepassword=mydr["password"].ToString();}
    else
    return false;
    if (thepassword==apassword)
    return true;
    else
    return false;
    }

    //检查账号的状态
    private void CheckState(string auser_id)
    {
    SqlConnection myconn=SQLConn(ConfigurationSettings.AppSettings["ConnectString"]);
    SqlDataReader mydr=SqlDr("select active from sys_user where user_id='"+auser_id+"' and deadline>getdate()",myconn);
    string active="False";
    if (mydr.Read())
    {
    if (!(mydr.IsDBNull(0)))
    active=mydr["active"].ToString();}
    else
    ErrorMessage("账户已经过期!");
    mydr.Close();    
    myconn.Close();
    if (active=="True")
    {
    WriteLog(auser_id); 
    Session["User_id"]=auser_id;
    Response.Write("<script language=javascript>window.open('index.aspx', '', 'top=0; left=0; height=710,width=1014,toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no,fullscreen=7' );window.opener='null';window.close()</script>");
    }
    else
    ErrorMessage("账户已经被禁止使用!");   
    }

      

  4.   

    //页面被载入时
    private void Page_Load(Object sender, EventArgs e) 
    {
    Session["refresh"]="";
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.btn_login.Click += new System.Web.UI.ImageClickEventHandler(this.btn_login_Click);
    this.btn_cancel.Click += new System.Web.UI.ImageClickEventHandler(this.btn_cancel_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    private void btn_login_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
    DateTime dt=new DateTime ();
    string sd="2003-9-30";
    dt=Convert.ToDateTime (sd);
    if(Convert.ToDateTime (DateTime.Now .ToString())>dt )
    {
    ErrorMessage("版本过期");
    return;
    }//              string num=Public_Class.Judge_Repeat ("SELECT count(*) as iCount from sys_login");
    string userid=user_i.Text .Trim ();
    string  myConnectionString  =  ConfigurationSettings.AppSettings["ConnectString"]; 
    string mySel="SELECT count(*) as iCount from sys_login where user_id="+"'"+userid+"'"; 
                                                                   //查看sys_login表中是否已存在该user_id
    SqlConnection  myConnection  =  new  SqlConnection(myConnectionString);   
    SqlCommand  myCommand  =  new  SqlCommand  (mySel,myConnection);
    myCommand.Connection.Open(); SqlDataReader Dr1;
    Dr1=myCommand .ExecuteReader();
    Dr1.Read();
    string Count=Dr1["iCount"].ToString();
    Dr1.Close();
    myCommand.Connection.Close();

    if(Count!="0")                  //如果sys_login中存在该user_id(表示用户已在线),查处相应的user_name
    {
    SqlConnection myConn = new SqlConnection( ConfigurationSettings.AppSettings["ConnectString"]);
    string strSQL="SELECT user_name FROM sys_user where user_id="+"'"+userid+"'";
    SqlDataAdapter myCmd = new SqlDataAdapter(strSQL, myConn);
    DataSet ds = new DataSet();
    myCmd.Fill(ds,"t1");
    myConn.Close ();
                                                                           //查处相应的login_time,login_ip 
    SqlConnection myConn1 = new SqlConnection( ConfigurationSettings.AppSettings["ConnectString"]);
    string strSQL1="SELECT login_time,login_ip FROM sys_login where user_id="+"'"+userid+"'";
    SqlDataAdapter myCmd1 = new SqlDataAdapter(strSQL1, myConn1);
    // DataSet ds = new DataSet();
    myCmd1.Fill(ds,"t2");
    myConn1.Close (); string ConnectString = ConfigurationSettings.AppSettings["ConnectString"];
                                                            //将当前用户登陆信息插入到sys_login_his
    string SqlString = "insert into sys_login_his values("+"'"+userid+"',"+"'"+ds.Tables ["t2"].Rows[0][1].ToString ()+"',"+"'"+DateTime.Now.ToString()+"',"+"'"+ds.Tables ["t2"].Rows[0][0].ToString ()+"',"+"'"+ds.Tables ["t1"].Rows[0][0].ToString ()+"')";
    SqlConnection dbConnection = new SqlConnection(ConnectString);
    SqlCommand dbCommand = new SqlCommand(SqlString,dbConnection);
    try 
    {
    dbConnection.Open();
    dbCommand.ExecuteNonQuery();
    }
    //捕获数据库操作错误
    catch 
    {
    //Label1.Text = e.Message;
    }
    finally
    {
    dbCommand.Dispose();
    dbConnection.Close();
    dbConnection.Dispose();
    }
                                                                                 //将当前用户登陆信息从sys_login删除
    string  SQLStatement="delete from sys_login where user_id="+"'"+userid+"'";
    SqlConnection  myConnection1  =  new  SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);   
    SqlCommand  myCommand1  =  new  SqlCommand  (SQLStatement,myConnection1);
    myCommand1.CommandTimeout  =  15;   
    myCommand1.CommandType=CommandType.Text;    try   
    {   
    myConnection1.Open();   
    myCommand1.ExecuteNonQuery();   
    myConnection1.Close();   


    }   
    catch  
    {   
      
    }   
                                                          // //将现在用户登陆信息插入sys_login
    string SqlString3 = "insert into sys_login values("+"'"+userid +"',"+"'"+DateTime.Now .ToShortDateString ()+"',"+"'"+"'"+Request.ServerVariables["REMOTE_ADDR"].ToString ()+"')";
    SqlConnection dbConnection3 = new SqlConnection(ConnectString);
    SqlCommand dbCommand3 = new SqlCommand(SqlString3,dbConnection3);
    try 
    {
    dbConnection3.Open();
    dbCommand3.ExecuteNonQuery();
    }
    //捕获数据库操作错误
    catch 
    {
    //Label1.Text = e.Message;
    }
    finally
    {
    dbCommand3.Dispose();
    dbConnection3.Close();
    dbConnection3.Dispose();
    } }
    string auser_id=user_i.Text;
    string apassword=pass_word.Text;
    string count=""; SqlConnection myconn=SQLConn(ConfigurationSettings.AppSettings["ConnectString"]);
    SqlDataReader mydr=SqlDr("select count(user_id) as cnt from sys_user",myconn);

    if (mydr.Read())
    if (!(mydr.IsDBNull(0)))
    count=mydr["cnt"].ToString();
    mydr.Close();    
    myconn.Close();
    if (count=="0")
    {
    //添加一个admin账户
    myconn.Open();
    ExcuteComm("insert into sys_user(user_id,user_name,sys_power,deg_power,deadline,password,active) values('admin','系统管理员',1,1,'2999-12-31','x',1)",myconn);
    ErrorMessage("系统第一次使用,请用管理员(admin)身份登陆!");
        myconn.Close();
    }

    else
    {
    if (IsMatch(auser_id,apassword))
    {
    if (apassword=="")
    {

    Session["User_id"]=auser_id;
    Response.Redirect("set_password.aspx?user_id="+auser_id);
    // Response.Redirect("set_password.aspx?user_id="+auser_id);
    }
    else
    CheckState(auser_id);
    }
    else
    ErrorMessage("用户名或者密码错误!");
    }  } private void btn_cancel_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
    ReSetForm();
    //this.Unload ;
    //Response.Redirect("set_password.aspx");
    } }
    }
      

  5.   

    TO yingkouliuyi(留意) 
    我的伊妹儿是[email protected]
      

  6.   

    登录的时候需要用session或cookies吗?
      

  7.   

    为什么我用ErrorMessage("账户已经被禁止使用!");会出错,是需要引用什么单元吗?