📄 login.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
/// <summary>
/// 2008年7月17日 谈鸿如 编写
/// 2008年7月17日 测试
/// 用于实现登录功能
/// </summary>
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//当点击登录按钮时调用下面事件
protected void memberlogin_Click1(object sender, ImageClickEventArgs e)
{
try
{
using (SqlConnection conn = new SqlConnection(_connectString)) //建立连接
{
conn.Open(); //打开连接
using (SqlCommand cmd = conn.CreateCommand())
{
//判断tb_User表中的userpwd字段中的值是否与页面上所填写的一致
cmd.CommandText = "select userpwd from dbo.tb_user where username=@username";
cmd.Parameters.Add("@username", SqlDbType.NVarChar).Value = username.Text;
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
code codelogin = new code(); //使用加密类
string originalpwd = codelogin.Decrypt(reader.GetString(0));
if (String.CompareOrdinal(this.userpwd.Text, originalpwd) == 0)
{
Session["username"] = username.Text;
Response.Redirect("Default.aspx",false );
}
else
{
//密码错误时的错误信息
Response.Write(@"<script>alert('密码错误');</script>");
}
}
else
{
//用户名错误时的错误信息
Response.Write(@"<script>alert('无此用户');</script>");
}
}
}
}
}
//若查询数据库失败则抛出异常信息
catch (Exception ex)
{
Response.Write(@"<script>alert('系统错误');</script>");
}
}
//使用db_sellConnectionString连接字符串
private static readonly string _connectString = System.Configuration.ConfigurationManager.ConnectionStrings["db_sellConnectionString"].ConnectionString;
//点击重置按钮时,将用户名、密码清空
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
username.Text = "";
userpwd.Text = "";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -