ASP.NET用户登陆验证
1.1登录界面前端代码展示(用户控件)
%@ Control Language="C#" AutoEventWireup="true" CodeFile="landing.ascx.cs" Inherits="controls_WebUserControl" %>
<div>
<center>
<asp:Label ID="tishi" Text="欢迎登陆本系统" runat="server" ForeColor="Red"></asp:Label>
<table style="height: 86px; width: 319px">
<tr>
<td>用户名</td>
<td><asp:TextBox ID="txt_user" runat="server" Text="" MaxLength="11"></asp:TextBox>
</td>
</tr>
<tr>
<td>密码</td>
<td><asp:TextBox ID="txt_password" runat="server" Text="" MaxLength="11" TextMode="password"></asp:TextBox></td>
</tr>
</table>
<asp:Button ID="btn_landing" Text="登录" runat="server" Width="95px"
Height="26px" onclick="btn_landing_Click"/>
</center>
</div>
1.2后端.cs文件代码展示
using System;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
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;
using System.Data;
public partial class controls_WebUserControl : System.Web.UI.UserControl
{
static string strCon = "Server=localhost;Initial Catalog=se;Trusted_Connection=yes;"; // 连接windows身份验证模式
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
}
protected void btn_landing_Click(object sender, EventArgs e)
{
con.Open();
string userName= txt_user.Text.Trim();
string userPassword = txt_password.Text.Trim();
cmd.CommandText = "Select * from logo where User_id='" + userName + "' and User_password='" + userPassword + "'";
SqlDataReader dr = cmd.ExecuteReader();
if (txt_user.Text == "system") //验证用户输入用户名是否为管理员
{
Response.Redirect("~/Default1.aspx");
}
if (txt_user.Text == "jhq")
{
Response.Redirect("~/Default1.aspx");
}
if (dr.Read()) //验证用户输入用户名是否为访客
{
//验证成功
Response.Redirect("~/Default2.aspx");
}
else
{
//验证失败
Literal txtMsg = new Literal();
txtMsg.Text = "
";
Page.Controls.Add(txtMsg);
}
dr.Close();
con.Close();
}
}