manage.aspx.cs
public partial class manage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string name = Request.Form.Get("name");
string paw = Request.Form.Get("paw");
if (name == "" || paw == "") //如果用户输入空值时
Response.Write("请返回输入用户名或密码");
else
{
//连接数据库
string connStr = "server=localhost;database=users;User Id=squall;Pwd=abcdefg123";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
//执行一个查询是否存在用户的SQL语句
string sqlStr="select count(*) from Login where userName='"+name+"' and userPaw='"+paw+"'";
SqlCommand comm = new SqlCommand(sqlStr,conn);
int count = (int)comm.ExecuteScalar();
if (count == 0)
Response.Write("没有这个用户或密码错误!");
else
{
//存在这个用户时,则同时更新Log表
sqlStr = "insert into Log(userId,LogTime,ip) select Login.userId,getdate(),'" + Request.ServerVariables.Get("REMOTE_ADDR") + "' from Login where Login.userName='" + name + "'";
comm.CommandText = sqlStr;
comm.ExecuteNonQuery();
Response.Write("<br/>登录信息插入日志表的SQL语句:"+sqlStr);
Response.Write("<br/><br/>成功登录");
}
conn.Close();
}
}
}
}
log_on.html
<body>
<h5>登录页面 (未注册的请:<a href="login.html">注册</a>)</h5>
<hr />
<form action="manage.aspx" method="post">
<table width="822" border="0" cellpadding="2" cellspacing="2" class="all">
<!--DWLayoutTable-->
<tr>
<td width="511" height="33" align="right" valign="middle">用户名:</td>
<td colspan="2" align="left" valign="middle"><input name="name" type="text" size="30" maxlength="30" /></td>
<td width="21"></td>
</tr>
<tr>
<td height="33" align="right" valign="middle">密码:</td>
<td colspan="2" align="left" valign="middle"><input name="paw" type=password size="30" maxlength="30" /></td>
<td></td>
</tr>
<tr>
<td height="26"></td>
<td width="109" valign="top"><input type="submit" name="Submit" value="提交" /></td>
<td width="155" valign="top"><input type="reset" name="Submit2" value="重置" /></td>
<td></td>
</tr>
<tr>
<td height="23"></td>
<td> </td>
<td></td>
<td></td>
</tr>
</table>
</form>
</body>
deal.aspx
public partial class deal : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string loginName = Request.Form.Get("loginName");
string loginPaw = Request.Form.Get("loginPaw");
string loginPaw2 = Request.Form.Get("loginPaw2");
if (loginName == "" || loginPaw == "" || loginPaw != loginPaw2)
Response.Write("输入出现错误");
else
{
//连接数据库
string connStr = "server=localhost;database=users;User Id=squall;Pwd=abcdefg123";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
//执行一个查询是否存在用户的SQL语句
string sqlStr = "select count(*) from Login where userName='" + loginName + "'";
SqlCommand comm = new SqlCommand(sqlStr, conn);
int count = (int)comm.ExecuteScalar();
if (count != 0)
Response.Write("已经存在这个用户名了!");
else
{
sqlStr = "insert into Login(userName,userPaw) values('" + loginName + "','" + loginPaw + "')";
Response.Write(sqlStr);
comm.CommandText = sqlStr;
comm.ExecuteNonQuery();
sqlStr = "insert into Log(userId,LogTime,ip) select Login.userId,getdate(),'" + Request.ServerVariables.Get("REMOTE_ADDR") + "' from Login where Login.userName='" + loginName + "'";
comm.CommandText = sqlStr;
comm.ExecuteNonQuery();
Response.Write("<br/>"+sqlStr);
Response.Write("<br/><br/>" + "注册成功,正准备转向主页");
}
conn.Close();
}
}
}
}
login.html
<body>
<h5>注册页面 (已注册的请:<a href="log_on.html">登录</a>)</h5>
<hr />
<form action="deal.aspx" method="post">
<table width="458" border="0" cellpadding="0" cellspacing="5" class="all">
<!--DWLayoutTable-->
<tr>
<td width="180" height="32" align="right" valign="top">用户名:</td>
<td colspan="2" valign="top"><input type="text" name="loginName" /></td>
</tr>
<tr>
<td height="32" align="right" valign="top">密码:</td>
<td colspan="2" valign="top"><input name="loginPaw" type="password"/></td>
</tr>
<tr>
<td height="30" align="right" valign="top">确认密码:</td>
<td colspan="2" valign="top"><input name="loginPaw2" type="password" /></td>
</tr>
<tr>
<td height="46"></td>
<td width="118" ><input type="submit" name="Submit" value="提交" /></td>
<td width="140" ><input type="reset" name="Submit2" value="重置" /></td>
</tr>
</table>
</form>
</body>
本文介绍了一个使用ASP.NET实现的简单登录与注册系统。该系统包括前后端交互过程,利用SQL Server进行用户验证及记录登录日志。文章详细展示了如何通过C#处理登录请求并检查用户凭据。
914

被折叠的 条评论
为什么被折叠?



