标题:C#做的ASP.NET登錄篇[转]
只看楼主
冰封谷主
Rank: 4
等 级:贵宾
威 望:10
帖 子:226
专家分:20
注 册:2005-1-7
 问题点数:0 回复次数:4 
C#做的ASP.NET登錄篇[转]
一、新建一个数据库
  新建一个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.
</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
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="没有这个用户.";
  }
 }
}
搜索更多相关主题的帖子: NET ASP 
2005-08-06 17:25
virusswb
Rank: 1
等 级:新手上路
威 望:1
帖 子:342
专家分:0
注 册:2005-8-6
得分:0 
[分享]谢谢版主
谢谢版主

jorden008@
2005-08-07 14:35
virusswb
Rank: 1
等 级:新手上路
威 望:1
帖 子:342
专家分:0
注 册:2005-8-6
得分:0 
.NET IDE环境的菜单栏里的框架菜单怎么是灰的,不可用,请问怎么回事?
谢谢!

jorden008@
2005-08-07 16:33
冰封谷主
Rank: 4
等 级:贵宾
威 望:10
帖 子:226
专家分:20
注 册:2005-1-7
得分:0 
检查一下你配置的iis服务是否正确/

2005-08-08 08:50
hsr2008
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-12-28
得分:0 
出现问题
string dpw=dr["password"].tostirng();
为什么会出错呢 提示没有数据的读取 明明是有数据的
2005-12-28 20:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-24534-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.695611 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved