asp.net登陆验证怎么实现呢

2025-03-10 01:34:36
推荐回答(3个)
回答1:

1、先连接数据库,建一个判断用户名是否存在的类,返回布尔类型的值。简单写一下:select * from 用户表 where username=控件内的值,判断数据表的Rows.count是否>0,返回True或False;2、在登陆的Click事件中写个if条件嵌套语句,第一层判断返回true或者false,第二层判断用户名或密码是否正确,简单写一下:bool result= 判断是否存在;if(result=false){ if(用户名密码正确) { 执行操作; } else { messagebox.show("用户名或密码错误!") } }else{ messagebox.show("用户名已注册!")} 写得很简单,希望对你有所帮助!

回答2:

控件你都拖出来了,你在拖个数据源,然后绑定下,废话不啰嗦!

回答3:

我以前自己编的,你自己看吧,看不明白再追!
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;

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

}
protected void Button1_Click(object sender, EventArgs e)
{

string connString = Convert.ToString(ConfigurationManager.ConnectionStrings["001ConnectionString"]);
//001ConnectionString是我在webconfig里配置的数据库连接。
SqlConnection conn = new SqlConnection(connString);
string strsql = "select * from User_table where User_name='" + UserName.Text + "' and Password='" + Password.Text + "'";
SqlCommand cmd = new SqlCommand(strsql, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

if (dr.Read())
{
Response.Redirect("index.aspx");
conn.Close();
}
else
{
FailureText.Text = "登陆失败,请检查登陆信息!";
conn.Close();
Response.Write("");
}
}

protected void Button2_Click(object sender, EventArgs e) //文本框重置按钮
{
UserName.Text = "";
Password.Text = "";

}
}

下面是aspx页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>





无标题页























用户登陆

用户名:


密码: