首页 > 编程 > .NET > 正文

C#做的ASP.NET登錄篇

2024-07-10 13:00:07
字体:
来源:转载
供稿:网友
一、新建一个数据库
  新建一个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" runat="server" width="55px"



height="26px">登录名</asp:label>
<asp:label id="label2" runat="server" width="63px" height="24px">密 码</asp:label>
<asp:textbox id="userid" runat="server" width="109px" height="25px"></asp:textbox>
<asp:textbox id="pwd" runat="server" width="109px" height="22px" textmode="password"></asp:textbox>
<asp:button id="logbutton" runat="server" width="59px" height="25px" text="登 录"></asp:button>
<asp:label id="msg" runat="server" width="117px" height="26px"></asp:label>
<asp:requiredfieldvalidator id="requiredfieldvalidator1" runat="server" width="162px" height="18px" errormessage="requiredfieldvalidator" controltovalidate="userid">请输入登录名!</asp:requiredfieldvalidator>
<asp:requiredfieldvalidator id="requiredfieldvalidator2" 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="没有这个用户.";
  }
 }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表