login.aspx:::
<body style="BACKGROUND-IMAGE: url(images/index.gif); BACKGROUND-REPEAT: no-repeat" leftMargin="0"
topMargin="0">
<table height="450" width="450" border="0">
<tr>
<td borderColor="#ffffff">
<form id="Form1" runat="server">
<table align="left">
<tr>
<td height="13" style="HEIGHT: 13px">用户类型</td>
<td style="HEIGHT: 13px"><SELECT id="UserType" style="WIDTH: 80px" name="" runat="server">
<OPTION value="Patient" selected>病人</OPTION>
<OPTION value="Doctor">医生</OPTION>
<OPTION value="Admin">管理员</OPTION>
</SELECT></td>
<td style="HEIGHT: 13px"></td>
<td style="HEIGHT: 13px"><asp:checkbox id="chkPersistLogin" runat="server"></asp:checkbox>记住我的登陆</td>
<td style="HEIGHT: 13px"><FONT face="宋体"> </FONT></td>
<td style="HEIGHT: 13px"><FONT face="宋体"> </FONT></td>
</tr>
<tr>
<td height="18" >用户名</td>
<td height="18" >
<input id="UserID" style="WIDTH: 80px; HEIGHT: 17px" type="text" size="8" name="UserID"
runat="server"></td>
<TD height=18 ?></TD>
<td height="18" colSpan="1" ><FONT face="宋体">
<asp:CheckBox id="CheckBox2" runat="server" AutoPostBack="True"></asp:CheckBox>用户注册</FONT>
</td>
<td height="18" ></td>
<TD height=18>
<asp:button id=ButSubmit onclick=ButSubmit_Click runat="server" text="登陆"></asp:button></TD>
</tr>
<TR>
<TD style="HEIGHT: 15px" align=center colSpan=1>
<P align=left>密码 </P></TD>
<TD style="HEIGHT: 15px"><INPUT id=UserPassword
style="WIDTH: 80px; HEIGHT: 17px" type=password size=6
name=UserPassword runat="server"></TD>
<TD style="HEIGHT: 15px"><ASP:REQUIREDFIELDVALIDATOR id="Requiredfieldvalidator2" runat="server" ErrorMessage="*" Display="Static" ControlToValidate="UserPassword"></ASP:REQUIREDFIELDVALIDATOR></TD>
<td style="HEIGHT: 15px"><FONT face="宋体">
<asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"></asp:CheckBox>游客参观</FONT></td>
<td style="HEIGHT: 15px" align="center"><FONT face="宋体"> </FONT></td></TR>
<tr>
<td colSpan="5"><asp:label id="LabMessage" runat="server" Font-Size="10" Font-Name="Verdana" ForeColor="red"></asp:label></td>
</tr>
<tr>
<td colSpan="5"><span align="center">试用帐号、密码如下</span><br>
<li>
医生帐号:2 密码:222
<li>
病人帐号:004 密码:004
<li>
管理员帐号:1 密码:zzj</li></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
<body style="BACKGROUND-IMAGE: url(images/index.gif); BACKGROUND-REPEAT: no-repeat" leftMargin="0"
topMargin="0">
<table height="450" width="450" border="0">
<tr>
<td borderColor="#ffffff">
<form id="Form1" runat="server">
<table align="left">
<tr>
<td height="13" style="HEIGHT: 13px">用户类型</td>
<td style="HEIGHT: 13px"><SELECT id="UserType" style="WIDTH: 80px" name="" runat="server">
<OPTION value="Patient" selected>病人</OPTION>
<OPTION value="Doctor">医生</OPTION>
<OPTION value="Admin">管理员</OPTION>
</SELECT></td>
<td style="HEIGHT: 13px"></td>
<td style="HEIGHT: 13px"><asp:checkbox id="chkPersistLogin" runat="server"></asp:checkbox>记住我的登陆</td>
<td style="HEIGHT: 13px"><FONT face="宋体"> </FONT></td>
<td style="HEIGHT: 13px"><FONT face="宋体"> </FONT></td>
</tr>
<tr>
<td height="18" >用户名</td>
<td height="18" >
<input id="UserID" style="WIDTH: 80px; HEIGHT: 17px" type="text" size="8" name="UserID"
runat="server"></td>
<TD height=18 ?></TD>
<td height="18" colSpan="1" ><FONT face="宋体">
<asp:CheckBox id="CheckBox2" runat="server" AutoPostBack="True"></asp:CheckBox>用户注册</FONT>
</td>
<td height="18" ></td>
<TD height=18>
<asp:button id=ButSubmit onclick=ButSubmit_Click runat="server" text="登陆"></asp:button></TD>
</tr>
<TR>
<TD style="HEIGHT: 15px" align=center colSpan=1>
<P align=left>密码 </P></TD>
<TD style="HEIGHT: 15px"><INPUT id=UserPassword
style="WIDTH: 80px; HEIGHT: 17px" type=password size=6
name=UserPassword runat="server"></TD>
<TD style="HEIGHT: 15px"><ASP:REQUIREDFIELDVALIDATOR id="Requiredfieldvalidator2" runat="server" ErrorMessage="*" Display="Static" ControlToValidate="UserPassword"></ASP:REQUIREDFIELDVALIDATOR></TD>
<td style="HEIGHT: 15px"><FONT face="宋体">
<asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True"></asp:CheckBox>游客参观</FONT></td>
<td style="HEIGHT: 15px" align="center"><FONT face="宋体"> </FONT></td></TR>
<tr>
<td colSpan="5"><asp:label id="LabMessage" runat="server" Font-Size="10" Font-Name="Verdana" ForeColor="red"></asp:label></td>
</tr>
<tr>
<td colSpan="5"><span align="center">试用帐号、密码如下</span><br>
<li>
医生帐号:2 密码:222
<li>
病人帐号:004 密码:004
<li>
管理员帐号:1 密码:zzj</li></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
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.SqlClient;
using System.Web.Security;namespace WebApplication
{
/// <summary>
/// Summary description for login.
/// </summary>
public class login : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected SqlConnection MyConnection;
protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator Requiredfieldvalidator2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button ButSubmit;
protected System.Web.UI.HtmlControls.HtmlInputText UserID;
protected System.Web.UI.HtmlControls.HtmlSelect UserType;
protected string MyConnectionString;
protected string sUserPassword;
protected System.Web.UI.WebControls.Label LabMessage;
protected System.Web.UI.WebControls.CheckBox chkPersistLogin;
protected System.Web.UI.HtmlControls.HtmlInputText UserPassword;
protected System.Web.UI.WebControls.CheckBox CheckBox1;
protected System.Web.UI.WebControls.CheckBox CheckBox2;
protected SqlDataReader reader;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ButSubmit.Click += new System.EventHandler(this.ButSubmit_Click);
this.CheckBox1.CheckedChanged += new System.EventHandler(this.CheckBox1_CheckedChanged);
this.CheckBox2.CheckedChanged += new System.EventHandler(this.CheckBox2_CheckedChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion protected void ButSubmit_Click(object sender, System.EventArgs e)
{
MyConnection = new SqlConnection();
MyConnection.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["SqlConnectionString"];
try
{
if(UserType.Value=="Doctor")
{
string MySelectString = @"Select DoctorPassword from Doctors Where DoctorID= '"+Convert.ToInt16(UserID.Value)+"'";
SqlCommand MyCommand = new SqlCommand(MySelectString,MyConnection);
MyCommand.Connection.Open();
reader = MyCommand.ExecuteReader();
while(reader.Read())
{
sUserPassword = reader.GetString(0);
}
MyCommand.Connection.Close();
if(UserPassword.Value==sUserPassword)
{
Getin();
}
else Getout(); } if(UserType.Value=="Patient")
{
string MySelectString = @"Select PatientPassword from Patients Where PatientUserID= '"+UserID.Value+"'";
SqlCommand MyCommand = new SqlCommand(MySelectString,MyConnection);
MyCommand.Connection.Open();
reader = MyCommand.ExecuteReader();
while(reader.Read())
{
sUserPassword = reader.GetString(0);
}
MyCommand.Connection.Close();
if(UserPassword.Value==sUserPassword)
Getin();
else Getout();
} if(UserType.Value=="Admin")
{
string MySelectString = @"Select AdminPassword from Admin Where AdminID= '"+Convert.ToInt16(UserID.Value)+"'";
SqlCommand MyCommand = new SqlCommand(MySelectString,MyConnection);
MyCommand.Connection.Open();
reader = MyCommand.ExecuteReader();
while(reader.Read())
{
sUserPassword = reader.GetString(0);
}
MyCommand.Connection.Close();
if(UserPassword.Value==sUserPassword) Getin();
else Getout();
}
if(UserType.Value=="Guest")
{
if(UserID.Value=="Guest"&&UserPassword.Value=="Guest")
Getin();
else Getout();
}
}
catch(Exception etc)
{
Getout();
} } protected void Getin()
{
HttpCookie cookie=new HttpCookie("aspx");
cookie["UserID"]=UserID.Value;
Session["UserID"]=UserID.Value;
Session["UserType"]=UserType.Value;
cookie["UserType"]=UserType.Value;
cookie["Userpassword"]=UserPassword.Value;;
Response.Cookies.Add(cookie);
FormsAuthentication.RedirectFromLoginPage(UserID.Value,chkPersistLogin.Checked);
}
protected void Getout()
{
LabMessage.Text = "凭据无效:请再试一次";
} private void CheckBox1_CheckedChanged(object sender, System.EventArgs e)
{
if(CheckBox1.Checked)
{
HttpCookie cookie=new HttpCookie("aspx");
cookie["UserType"]="Guest";
Response.Cookies.Add(cookie);
FormsAuthentication.RedirectFromLoginPage(UserID.Value,chkPersistLogin.Checked);
Response.Redirect("Guest_Default.aspx");
}
} private void CheckBox2_CheckedChanged(object sender, System.EventArgs e)
{
if(CheckBox2.Checked)
{
HttpCookie cookie=new HttpCookie("aspx");
cookie["UserType"]="Patient";
cookie["UserType1"]="Patient1";
Session["UserType"]="Patient";
FormsAuthentication.RedirectFromLoginPage(UserID.Value,chkPersistLogin.Checked);
Response.Redirect("Register1.aspx");
}
} }
}
谢谢~~
//主窗体Global
static void Main()
{
if((new Login()).ShowDialog() == DialogResult.OK) Application.Run(new Global());
}
//登陆窗体Login
if(true)this.DialogResult = DialogResult.OK;/*注销*/
private void mnuLogout_Click(object sender, System.EventArgs e)
{
this.Hide();
if((new Login()).ShowDialog() == DialogResult.OK)this.Show();
else Application.Exit();
}下面的是以前的贴子,搂主可以参考一下(包括登录成功后的用户信息如何传给主窗体?)
http://expert.csdn.net/Expert/topic/2024/2024955.xml?temp=3.544253E-02
int errornum=0;
Login frmLogin=new Login();//登录窗体,上面右textbox2个button2个
//设好button的dialogresult属性ok和cancel
again:
if(errornum>2) {//错误三次退出
this.Close();//关闭主窗体,程序自动退出
return;
}
frmLogin.Update();
if(frmLogin.ShowDialog()!=DialogResult.OK) {
this.Close();
return;
}
string username=frmLogin.textBox2.Text;
String pswd=frmLogin.textBox1.Text;
if(username!="admin"){
MessageBox.Show("用户不存在!");
errornum++;
goto again;
}
if("12345"!=pswd) {
MessageBox.Show("密码错误!");
errornum++;
goto again;
}
//成功的话会到达这里