最近一门课用的是asp.net,无奈只能去学它的代码,这里记录一段asp.net进行登录的代码:

Login.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Login.ascx.cs" Inherits="login" %>

<asp:Panel ID="Panel1" runat="server">
<table width="214" border="0" cellpadding="0" cellspacing="0" class="text">
<TR>
<TD>
<IMG height=57 src="images/denglu.jpg" width="214" border="0">
</TD>
</TR>
<tr>
<td align=center>
<table width=203px bgcolor=white cellpadding=0 cellspacing=0 class="text1">
<tr>
<td align=right width=30%>身份:</td>
<td align=left width=70%>
<asp:DropDownList ID="UserType" runat="server" Width="120">
<asp:ListItem Value="学生">学生</asp:ListItem>
<asp:ListItem Value="教师">教师</asp:ListItem>
<asp:ListItem Value="管理人员">管理人员</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td align=right>用户名:</td>
<td align=left>
<asp:TextBox ID="UserId" Runat="server" Width="120" /><font color="red" class="Normal">*</font>
<asp:RequiredFieldValidator id="RFVUserId" runat="server" ErrorMessage="<br>用户名不能为空。" ControlToValidate="UserId"
Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align=right>密码:</td>
<td align=left>
<asp:TextBox ID="Password" Runat="server" CssClass="InputText" Width="120" TextMode="Password" /><font color="red">*</font>
<asp:RequiredFieldValidator id="RFVPassword" runat="server" ErrorMessage="<br>密码不能为空。" ControlToValidate="Password"
Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align=right>验证码:</td>
<td align=left>
<asp:TextBox ID="Validator" Runat="server" Width="70"></asp:TextBox><font color="red">*</font>
<asp:Image ID="ValidateImage" runat="server" Height="25px" Width="50px" ImageAlign="AbsBottom" />
<asp:RequiredFieldValidator id="rfv" runat="server" ErrorMessage="<br>验证码不能为空。" ControlToValidate="Validator"
Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="LoginBtn" Runat="server" Text="登录" CssClass="ButtonCss" Width="70px" OnClick="LoginBtn_Click"></asp:Button>
</td>
</tr>
<tr>
<td colspan="2" align="center"><asp:Label ID="Message" Runat="server" CssClass="GbText" Width="100%" ForeColor="Red"></asp:Label></td>
</tr>
</table>
</td>
</tr>
<tr><td>
<IMG height=12 src="images/42.jpg" border="0">
</td></tr>
</table>
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" Height="50px" Width="100%">
<table width="214" border="0" class="text">
<tr>
<td align=left height=48 background="images/shuzhuo.jpg">
</td>
</tr>
<tr>
<td align=center>
<table width="203" bgcolor=white border="0" cellpadding=0 cellspacing=0 class="text">
<tr>
<td align=left>
   登录身份:<font class=text1>
<%=Session["UserType"]%></font>
</td>
</tr>
<tr>
<td align=left>
   用户姓名:
<font class=text1>
<%=Session["UserName"]%></font>
</td>
</tr>
<tr>
<td>
<hr />
</td>
</tr>
</table>
</td>
<tr><td>
<IMG height=12 src="images/42.jpg" width=214 border=0>
</td></tr>
</table>
</asp:Panel>

Login.aspx.cs

public partial class login : System.Web.UI.UserControl
{
private static string sValidator = ""; //验证字符串
private readonly string sValidatorImageUrl = "ValidateImage.aspx?Validator=";

protected void Page_Load(object sender, EventArgs e)
{
//检查系统是否已经登录,确定显示哪个Panel
if (Session["UserID"] != null)
{
//已经登录,显示“我的书桌”
this.Panel1.Visible = false;
this.Panel2.Visible = true;
}
else
{
//未登录
this.Panel2.Visible = false;
this.Panel1.Visible = true;

//如果是第一次加载页面,创建验证码
if (!Page.IsPostBack)
{
//创建验证字符串
sValidator = CreateValidateString(4);
//设置验证码图片的ImageUrl属性
ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;
}

//给出一个默认的用户类型。0-学生;1-老师;2-管理人员
this.UserType.SelectedIndex = 2;
}
}

//创建验证字符串
private string CreateValidateString(int nLen)
{
//创建一个StringBuilder对象
StringBuilder sb = new StringBuilder(nLen);
Random rnd = new Random();
int rndi = 1000 + rnd.Next(8999);
sb.Append(rndi.ToString());
return (sb.ToString());
}

//用户按“登录”按钮后进行验证
protected void LoginBtn_Click(object sender, EventArgs e)
{
//如果页面输入不合法
if (! Page.IsValid)return;

//如果验证码输入错误
if (Validator.Text != sValidator)
{
Message.Text = "验证码输入错误,请重新输入。";
//重新创建验证字符串
sValidator = CreateValidateString(4);
//重新设置验证码图片的ImageUrl属性
ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;
return;
}

String userId = "";
String userName = "";

//定义类并获取用户的登陆信息
User user = new User();

//对用户输入进行编码
//string sUserType = Server.HtmlEncode(UserType.SelectedItem.Value.ToString());
string sUserType = Request.Params["Login1$UserType"].ToString();
string sUserId = Server.HtmlEncode(UserId.Text.Trim());
string sPassword = Server.HtmlEncode(Password.Text.Trim());

//获取用户信息
SqlDataReader dr = user.GetUserLoginBySQL(sUserType, sUserId, sPassword);

//判断用户是否合法
if (dr.Read())
{
userId = dr["UserID"].ToString();
userName = dr["UserName"].ToString();
}
dr.Close();

//验证用户合法性,并跳转到系统平台
if ((userId != null) && (userId != ""))
{
//写会话对象
Session["UserType"] = sUserType;
Session["UserID"] = userId;
Session["UserName"] = userName;

//写Cookies
Response.Cookies["userInfo"]["UserType"] = sUserType;
Response.Cookies["userInfo"]["UserID"] = userId;
Response.Cookies["userInfo"]["UserName"] = userName;
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);

//跳转到登录后的系统主页
//Response.Redirect("~/mainframe.aspx");
Response.Write("<script>window.open('mainframe.aspx','_top');</script>");
}
else
{
//创建验证字符串
sValidator = CreateValidateString(4);
ValidateImage.ImageUrl = sValidatorImageUrl + sValidator;
//显示错误信息
Message.Text = "用户名或密码有误。";
}
}
}

连接数据库:

using System;
using System.Data;
using System.Configuration; // 读取 Web.config
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; //为了连接SQL Server数据库
//using System.Data.OracleClient; //为了连接Oracle数据库,还需要增加引用
using System.Data.OleDb; //为了连接OleDb数据库

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = "";

try
{
switch (DropDownList1.Text.ToString())
{
case "SQL Server":
connectionString = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString;
SqlConnection SQLConn = new SqlConnection(connectionString);
SQLConn.Open();
Response.Write("链接状态:" + (SQLConn.State == ConnectionState.Open ? "链接成功。" : "链接失败。") + "<br>");
Response.Write("链接字符串:" + SQLConn.ConnectionString + "<br>");
Response.Write("ConnectionTimeout:" + SQLConn.ConnectionTimeout.ToString() + "<br>");
Response.Write("DataSource:" + SQLConn.DataSource + "<br>");
Response.Write("ServerVersion:" + SQLConn.ServerVersion + "<br>");
SQLConn.Close();
break;
case "Access":
connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
OleDbConnection AccessConn = new OleDbConnection(connectionString);
AccessConn.Open();
Response.Write("链接状态:" + (AccessConn.State == ConnectionState.Open ? "链接成功。" : "链接失败。") + "<br>");
Response.Write("链接字符串:" + AccessConn.ConnectionString + "<br>");
Response.Write("ConnectionTimeout:" + AccessConn.ConnectionTimeout.ToString() + "<br>");
Response.Write("DataSource:" + AccessConn.DataSource + "<br>");
Response.Write("ServerVersion:" + AccessConn.ServerVersion + "<br>");
AccessConn.Close();
break;
default :
Response.Write("Please select a kind of Database.<br>");
break;
}
}
catch (Exception ex)
{
///显示连接错误的消息
Response.Write(ex.Message + "<br>");
}
}
}