ASP.NET的后台登录页面怎么实现?

flyso 2006-05-15 05:31:19
我看了好几个ASP.NET的后台登录页面代码,都看不到哪有连接数据库,和用SQL语句对用户输入的用户名和密码进行比较的,成功后写入Cookies和跳转到相关页面。。
请高手指点,或告诉哪有相关的好的源码可供参考学习的。。。
...全文
2023 39 打赏 收藏 转发到动态 举报
写回复
用AI写文章
39 条回复
切换为时间正序
请发表友善的回复…
发表回复
huyanjiang 2006-05-17
  • 打赏
  • 举报
回复
学习
kamson_cn 2006-05-17
  • 打赏
  • 举报
回复
我这是vb语法......
C#似乎是 Session["login"]
flyso 2006-05-17
  • 打赏
  • 举报
回复
谢谢楼上的无错代码,已经OK了,不过Session那部分有问题

If(count > 0){
string UserName;//我不知道申明这个有什么用?
Session("login") = True;
conn.Close();
}


然后需要加密的叶子加上:
If(Session("login") <> True){
response.redirct("error.aspx");
}


----------------------------------------

提示Session("login") = True;这一行有问题

编译器错误信息: CS0118: “System.Web.UI.Page.Session”是“属性”,但此处被当做“方法”来使用




careast 2006-05-17
  • 打赏
  • 举报
回复
嗯,是我倏忽了,kamson_cn(剽而学之) 说的对



flyso 2006-05-16
  • 打赏
  • 举报
回复
谢谢 careast ,你能不能直接帮我把
SqlConnection conn = New SqlConnection("server=.;database=user;uid=sa;pwd=");
conn.Open();
SqlCommand cmd=New SqlCommand("select count(*) from db_user where userid='" + UserID + "' and password='" + UserPwd + "'", conn);
改成连接Access的形式的
因为刚则学,对连接数据库还不是很熟悉
careast 2006-05-16
  • 打赏
  • 举报
回复
哦,忘了你是用ACCESS的了,要改的几个地方是:
SqlConnection conn = New SqlConnection("server=.;database=user;uid=sa;pwd=");
SqlCommand cmd=New SqlCommand("select count(*) from db_user where userid='" + UserID + "' and password='" + UserPwd + "'", conn);

不过这类的资料还是蛮多的,你自己可以找找看,不行再问。
careast 2006-05-16
  • 打赏
  • 举报
回复
我来帮你翻译一下好了:
private void Button1_Click(object sender, System.EventArgs e){
string UserID;
string UserPwd;
UserID = txtUserID.Text.Trim();
UserPwd = txtUserPwd.Text.Trim();
SqlConnection conn = New SqlConnection("server=.;database=user;uid=sa;pwd=");
conn.Open();
SqlCommand cmd=New SqlCommand("select count(*) from db_user where userid='" + UserID + "' and password='" + UserPwd + "'", conn);
int count;
count = Convert.ToInt32(cmd.ExecuteScalar());
If(count > 0){
string UserName;//我不知道申明这个有什么用?
Session("login") = True;
conn.Close();
}


然后需要加密的叶子加上:
If(Session("login") <> True){
response.redirct("error.aspx");
}
flyso 2006-05-16
  • 打赏
  • 举报
回复
谢谢楼上两位,因为我是初学,而且学的是C#,数据库用的是Access
kamson_cn兄你那代码C#版怎么写呢,请指点!
yhtapmys 2006-05-16
  • 打赏
  • 举报
回复
可运行的无错代码:
using System;
using System.Data;
using System.Data.OleDb;

namespace WebUI
{
/// <summary>
/// Test
/// </summary>

private void Button1_Click(object sender, System.EventArgs e)//按钮事件
{
string UserID;
string UserPwd;
UserID = Uid.Text.Trim();
UserPwd = Pwd.Text.Trim();
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";strConnection+=Server.MapPath("DB.mdb"); //*就是数据库的名字//嗯~~
OleDbConnection objConnection=new OleDbConnection(strConnection);
objConnection.Open();//打开数据库
OleDbCommand cmd = new OleDbCommand("select count(*) from admin where uid='" + UserID + "' and pwd='" + UserPwd + "'",objConnection);//对Sql Server才用SqlServer
int count;
count = Convert.ToInt32(cmd.ExecuteScalar());
if(count > 0)
{
string UserName;//我不知道申明这个有什么用?//嗯~~
Response.Write("<script language=javascript>alert('Sucess!')</script>");
objConnection.Close();//要记得关
}
}
}
cheneymilan 2006-05-16
  • 打赏
  • 举报
回复
学习
flyso 2006-05-16
  • 打赏
  • 举报
回复
有高手能贴出无错的C#源码吗
zzf1983 2006-05-16
  • 打赏
  • 举报
回复
这样跟的登陆,跟什么隐藏代码有联系吗,还不是一样能看到登陆程序!!!!!
unotfish 2006-05-16
  • 打赏
  • 举报
回复
.net下是好不容易才成功的
flyso 2006-05-16
  • 打赏
  • 举报
回复
改了之后错误依旧,还是那一行有错误
careast 2006-05-16
  • 打赏
  • 举报
回复
错了
错误在
SqlCommand cmd=New OleDbCommand("select count(*) from admin where uid='" + UserID + "' and pwd='" + UserPwd + "'",objConnection);


应该改为
OleDbCommand cmd=New OleDbCommand("select count(*) from admin where uid='" + UserID + "' and pwd='" + UserPwd + "'",objConnection);
flyso 2006-05-16
  • 打赏
  • 举报
回复
unotfish兄
能写出C#版吗!
unotfish 2006-05-16
  • 打赏
  • 举报
回复
<script Language="VB" runat="server">
Sub Login(sender As Object,e As Eventargs)
Dim objConn As New OleDbConnection()
objConn.connectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.Mappath("wwwlink.mdb"))
objConn.Open()
Dim objCmd As New OleDbCommand()
objCmd.Connection=objConn
objCmd.CommandText="Select * from admin where username='"& userID.Text & "' and password='"& userPwd.Text &"'"
'创建dataReader对象
Dim objReader As OleDbDataReader = objCmd.ExecuteReader()
'判断是否通过验证
If objReader.Read()=True Then
FormsAuthentication.RedirectFromLoginPage(userId.Text,savePwd.Checked)
Response.Redirect("default.aspx")
Else
errMessage.Text="帐号或密码错误,请重新输入"
End If
'关闭DataReader对象及数据连接
objReader.Close()
objConn.Close()
End Sub
</script>
flyso 2006-05-16
  • 打赏
  • 举报
回复
因为我只会asp,刚接触asp.net,感觉好多不一样,不知道怎么过渡
flyso 2006-05-16
  • 打赏
  • 举报
回复
那要怎么写啊!哪有相关代码啊~!
piaoyu228 2006-05-16
  • 打赏
  • 举报
回复
晕,这样的登录程序和直接登录有什么区别?
加载更多回复(19)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧