用户要登陆时,验证帐号是否存在,密码是否正确的代码怎么写?(100分)

  • 主题发起人 问题猫
  • 开始时间

问题猫

Unregistered / Unconfirmed
GUEST, unregistred user!
有两个文本框: txtID , txtPwd
数据库为ACCESS: db1库中的 users 表
首先判断 users 表中是否有txtID中输入的帐号,
然后判断密码与users 表中的密码是否相符。
 
J

janph

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么是c#的,不会用c#!
 
@

@念@

Unregistered / Unconfirmed
GUEST, unregistred user!
不会。。我还以为DELPHI。。
看来要开始学习C#。。
帮你顶。
 
B

book523

Unregistered / Unconfirmed
GUEST, unregistred user!
元旦后学C#
 
S

southman

Unregistered / Unconfirmed
GUEST, unregistred user!
我是SQL2000﹐用存儲過程﹐你要不要?
 

问题猫

Unregistered / Unconfirmed
GUEST, unregistred user!
如果是用delphi我就不用问了,
to southman,数据用sql2000
也可以,只要让我能实现帐号验证的功能就可以
 
P

proman

Unregistered / Unconfirmed
GUEST, unregistred user!
晕,这和用什么语言有关吗?
只要生成一句Sql:select Count(*) from table where userid='userid' and pswd='pswd'
如果返回值=1 Ok.否则失败.
在C#里执行这句和Delphi里不是差不多吗?
拖一个SqlConnection.建立一个SqlCommand对象.然后就OK了.
 
E

estorm

Unregistered / Unconfirmed
GUEST, unregistred user!
设表结构为:
users(id string,pwd string)
如下
string connstr="Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source="+Server.MapPath("db.mdb");
using(DataSet ds=new DataSet())
{
using (OleDbConnection conn=new OleDbConnection(connstr))
{
conn.Open();
string sql="select top 1 pwd from users where id='"+ txtID.Text +"'";
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
da.Fill(ds);
}
if (ds.Tables[0].Rows.Count>0)
{
if (ds.Tables[0].Rows[0]["pwd"].ToString().Equals(txtPwd.Text))
//认证成功
else
//密码错误
}
else
//用户名不存在
}
 
W

wrl_001

Unregistered / Unconfirmed
GUEST, unregistred user!
同意 proman  
反正思路都是用 用户名和密码 为条件去数据库中读取记录,
如果记录为0则失败,为1则成功,《应该没有其它可能了吧》???
 
E

Endy.Vee

Unregistered / Unconfirmed
GUEST, unregistred user!
呵呵,proman 的方法不妥,很容易进入系统的,存在着严重的安全问题,
同意estorm的方法,改动了一下[:D]
string connstr="Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source="+Server.MapPath("db.mdb");
OleDbConnection conn=new OleDbConnection(connstr);
conn.Open();
DataSet ds=new DataSet();
string sql="select top 1 pwd from users where id='"+ txtID.Text +"'";
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
da.Fill(ds);
if (ds.Tables[0].Rows.Count>0)
{
if (ds.Tables[0].Rows[0]["pwd"].ToString().Equals(txtPwd.Text))
//认证成功
else
//密码错误
}
else
//用户名不存在
}
 
W

wangxian11

Unregistered / Unconfirmed
GUEST, unregistred user!
毛啊,学的怎么样了??
图片上传的问题能不能解决阿
 

问题猫

Unregistered / Unconfirmed
GUEST, unregistred user!
to estorm,Endy.Vee
我试了试两位的方法,却出现了如下的错误?
问题出在哪里呢 ?
Server Error in '/myBBS' Application.
--------------------------------------------------------------------------------
至少一个参数没有被指定值。
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: 至少一个参数没有被指定值。
Source Error:

Line 63: OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
Line 64: DataSet ds=new DataSet();
Line 65: da.Fill(ds);
Line 66:
Line 67: if (ds.Tables[0].Rows.Count>0)

 
E

estorm

Unregistered / Unconfirmed
GUEST, unregistred user!
to 问题猫
检查一下你的sql语句中的字段名和表名是否和你的数据库中的一致
 
S

southman

Unregistered / Unconfirmed
GUEST, unregistred user!
一﹑存儲過程
create procedure User_Login
@user_id varchar(20),
@password varchar(50),
@Return_Status char(1) output,
@user_name varchar(20) output,
@dept varchar(20) output,
@duty varchar(20) output
AS

declare @dept_id varchar(4)
declare @duty_id varchar(4)
select @dept=''
select @duty=''
select @user_name=''
select @return_status='0' --不知明錯誤
if exists(select user_id from user_list where user_id=@user_id)
begin
if exists(select user_id from user_list where user_id=@user_id and password=@password)
begin
select @user_name=user_name,@dept_id=dept_id,@duty_id=duty_id from user_list where user_id=@user_id and password=@password
select @dept=sname from dept_list where dept_id=@dept_id
select @duty=sname from duty_list where duty_id=@duty_id
select @return_status='3' --正確登錄
end
else
select @return_status='2' --密碼錯誤
end
else
select @return_status='1' --用戶名不存在

GO

二﹑login.aspx(C#)
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.Configuration;
using System.Data.SqlClient;
namespace hterp
{
/// <summary>
/// Summary description for _Default.
/// </summary>
public class _Default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Msg;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected SqlConnection Scn;

private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack)
{
Msg.Visible=false;
}

}
#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.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
Common_Data conn=new Common_Data();
SqlCommand myComm=new SqlCommand("User_Login",conn.Common_Conn());
myComm.CommandType=CommandType.StoredProcedure;
myComm.Parameters.Add("@user_id",SqlDbType.NChar,20);
myComm.Parameters["@user_id"].Value=TextBox1.Text;
myComm.Parameters.Add("@password",SqlDbType.NChar,20);
myComm.Parameters["@password"].Value=TextBox2.Text;
myComm.Parameters.Add("@Return_Status",SqlDbType.Char,1);
myComm.Parameters.Add("@user_Name",SqlDbType.NChar,20);
myComm.Parameters.Add("@Dept",SqlDbType.NChar,20);
myComm.Parameters.Add("@duty",SqlDbType.NChar,20);
myComm.Parameters["@return_Status"].Direction=ParameterDirection.Output;
myComm.Parameters["@User_Name"].Direction=ParameterDirection.Output;
myComm.Parameters["@Dept"].Direction=ParameterDirection.Output;
myComm.Parameters["@duty"].Direction=ParameterDirection.Output;
try
{
myComm.Connection.Open();
SqlDataReader dr=myComm.ExecuteReader();
if (myComm.Parameters["@Return_Status"].Value.ToString()=="0") {
Msg.Visible=true;
Msg.Text="不知名錯誤﹐請與系統管理員聯系";
Response.Write(Functions.ShowErrMsg(Msg.Text));
}
if (myComm.Parameters["@Return_Status"].Value.ToString()=="1") {
Msg.Visible=true;
Msg.Text="用戶名不存在﹗";
Response.Write(Functions.ShowErrMsg(Msg.Text));
}
if (myComm.Parameters["@Return_Status"].Value.ToString()=="2") {
Msg.Visible=true;
Msg.Text="密碼錯誤﹗";
Response.Write(Functions.ShowErrMsg(Msg.Text));
}
if (myComm.Parameters["@Return_Status"].Value.ToString()=="3") {
Msg.Visible=false;
HttpCookie User_Cookie=new HttpCookie("User_Name");
User_Cookie.Value=myComm.Parameters["@user_Name"].Value.ToString();
Response.Cookies.Add(User_Cookie);
HttpCookie Dept_Cookie=new HttpCookie("Dept");
Dept_Cookie.Value=myComm.Parameters["@Dept"].Value.ToString();
Response.Cookies.Add(Dept_Cookie);
HttpCookie Duty_Cookie=new HttpCookie("Duty");
Duty_Cookie.Value=myComm.Parameters["@duty"].Value.ToString();
Response.Cookies.Add(Duty_Cookie);
//Response.Write("<script>window.open('http://192.168.0.200/hterp/default.aspx','new','fullscreen=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,width=790,height=538,resizable=0,top=0,left=0');
</script>");
Response.Redirect("http://192.168.0.200/hterp/default.aspx");
}
}
finally {
myComm.Dispose();
conn.Common_Conn().Close();
}
}
}
}
這是我本人為公司管理軟件寫的﹐沒有問題
試試吧
 

问题猫

Unregistered / Unconfirmed
GUEST, unregistred user!
[blue]谢谢 estorm 大侠指点![/blue]
我的问题解决了!
同时也感谢 Endy.Vee和southman
的关注!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
881
DelphiTeacher的专栏
D
顶部