vb语言asp.net登陆代码 急
急需用vb语言密码验证登陆网站的代码要连接数据库的有高人请指点一下最好是源代码谢谢了急需啊...
急需 用vb语言密码验证登陆网站的代码 要连接数据库的 有高人请指点一下最好是源代码 谢谢了 急需啊
展开
2个回答
展开全部
人家要vb的。。。一楼的不地道。
Partial Class Login
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
btnLogin.Enabled = True
lblMsg.Text = ""
If Session("loginName") <> "" Then
btnLogin.Enabled = False
lblMsg.Text = "Please Logout first."
End If
txtName.Focus()
End Sub
Protected Sub btnlogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim objDBUser As New cDBUser
If objDBUser.checkLoginName(txtName.Text) = 1 Then
Dim accessLv As Integer = objDBUser.checkPassword(txtName.Text, txtPwd.Text)
If accessLv = -1 Then
lblMsg.Text = "Wrong Password. Please try again."
txtPwd.Focus()
ElseIf accessLv = 0 Then
lblMsg.Text = "Your account is locked. Contact with site admin."
Else
lblMsg.Text = "Login successfully."
Dim loginName As String = txtName.Text
Session.Add("loginName", loginName)
Session.Add("userName", objDBUser.getUserName(loginName))
Response.Redirect("Index.aspx")
End If
Else
lblMsg.Text = "Wrong Login Name."
txtName.Focus()
End If
End Sub
Protected Sub btnLogout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogout.Click
Session.Clear()
End Sub
End Class
这个是code behind file
另外需要在app code 里面放一个叫dbUser和Cuser的class
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Public Class cDBUser
Private conn As New SqlConnection
Private connstr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Private sqlCmd As New SqlCommand
Public Sub New()
conn.ConnectionString = connstr
sqlCmd.Connection = conn
End Sub
Public Function checkLoginName(ByVal loginName As String) As Integer
Try
conn.Open()
sqlCmd.CommandText = "SELECT COUNT(password) FROM tblUser where LoginName = '" & loginName & "'"
Return sqlCmd.ExecuteScalar
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function checkPassword(ByVal loginName As String, ByVal pwd As String) As Integer
Try
conn.Open()
sqlCmd.CommandText = "SELECT COUNT(*) FROM tblUser WHERE loginName = '" & loginName & _
"' and password = '" & pwd & "'"
Dim passwordOK As Integer = sqlCmd.ExecuteScalar
If passwordOK = 1 Then
sqlCmd.CommandText = "SELECT accesslevel FROM tblUser WHERE loginName = '" & loginName & _
"' and password = '" & pwd & "' and status = 1"
Return CInt(sqlCmd.ExecuteScalar)
Else
Return -1
End If
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function getOldPwd(ByVal loginName As String) As String
Try
conn.Open()
sqlCmd.CommandText = "SELECT Password FROM tblUser WHERE LoginName = '" & loginName & "'"
Return sqlCmd.ExecuteScalar
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
Finally
conn.Close()
End Try
End Function
Public Function getUserName(ByVal loginName As String) As String
Try
conn.Open()
sqlCmd.CommandText = "SELECT UserName FROM tblUser WHERE LoginName = '" & loginName & "'"
Return sqlCmd.ExecuteScalar
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
Finally
conn.Close()
End Try
End Function
Public Function getAllUsers() As DataView
Try
conn.Open()
Dim sqlAdpt As New SqlDataAdapter("SELECT userID,LoginName,UserName,Email,AccessLevel,Status FROM tblUser", connstr)
Dim dt As New DataTable
sqlAdpt.Fill(dt)
Return dt.DefaultView
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
Finally
conn.Close()
End Try
End Function
Public Function getUserDetails(ByVal loginName As String) As cUser
Try
conn.Open()
Dim sqlAdpt As New SqlDataAdapter("SELECT * from tblUser where loginName = '" & loginName & "'", connstr)
Dim dt As New DataTable
sqlAdpt.Fill(dt)
Dim dr As DataRow = dt.Rows(0)
Dim objUser As New cUser
objUser.userId = dr.Item("UserId")
objUser.loginName = dr.Item("LoginName")
objUser.password = dr.Item("Password")
objUser.userName = dr.Item("UserName")
objUser.Email = dr.Item("Email")
objUser.accessLevel = dr.Item("AccessLevel")
objUser.status = dr.Item("Status")
Return objUser
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
Finally
conn.Close()
End Try
End Function
Public Function changeUserStatus(ByVal userId As Integer) As Integer
Try
conn.Open()
sqlCmd.CommandText = "UPDATE tblUser SET status = (1 - status) WHERE userId = " & userId
Return CInt(sqlCmd.ExecuteScalar)
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function changeEmail(ByVal loginName As String, ByVal newEmail As String) As Integer
Try
conn.Open()
sqlCmd.CommandText = "UPDATE tblUser SET Email = '" & newEmail & "' WHERE LoginName = '" & loginName & "'"
Return sqlCmd.ExecuteNonQuery
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function changePwd(ByVal loginName As String, ByVal newPwd As String) As Integer
Try
conn.Open()
sqlCmd.CommandText = "UPDATE tblUser SET password = '" & newPwd & "' WHERE loginName = '" & loginName & "'"
Return sqlCmd.ExecuteNonQuery
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function addUser(ByVal objUser As cUser) As Integer
Try
conn.Open()
sqlCmd.CommandText = "INSERT INTO tblUser (userName,loginName,password,email,accessLevel,status) VALUES('" & _
Trim(objUser.userName) & "','" & Trim(objUser.loginName) & "','" & objUser.password & "','" & Trim(objUser.Email) & "','" & _
objUser.accessLevel & "',1)"
If sqlCmd.ExecuteNonQuery() = 1 Then
sendEmail()
Return 1
End If
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function retrievePwd(ByVal loginName As String) As Integer
Try
conn.Open()
sqlCmd.CommandText = "SELECT password from tblUser where loginName = '" & loginName & "'"
Dim pwd As String = sqlCmd.ExecuteScalar
Dim email As String = getEmail(loginName)
If email <> "" And pwd <> "" Then
sendEmail()
Return 1
Else
Return 0
End If
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function getEmail(ByVal loginName As String) As String
conn.Close()
Try
conn.Open()
sqlCmd.CommandText = "SELECT Email from tblUser where loginName = '" & loginName & "'"
Dim email As String = sqlCmd.ExecuteScalar
Return email
Catch ex As Exception
MsgBox(ex.Message)
Return ""
Finally
conn.Close()
End Try
End Function
Private Sub sendEmail()
'some code here to send email
End Sub
End Class
Imports Microsoft.VisualBasic
Public Class cUser
Private intUserId As Integer
Private strUserName As String
Private strLoginName As String
Private strPassword As String
Private strEmail As String
Private shortAccessLevel As Short
Private byteStatus As Byte
Public Sub New()
End Sub
Public Property userId() As Integer
Get
Return intUserId
End Get
Set(ByVal value As Integer)
intUserId = value
End Set
End Property
Public Property userName() As String
Get
Return strUserName
End Get
Set(ByVal value As String)
strUserName = value
End Set
End Property
Public Property loginName() As String
Get
Return strLoginName
End Get
Set(ByVal value As String)
strLoginName = value
End Set
End Property
Public Property password() As String
Get
Return strPassword
End Get
Set(ByVal value As String)
strPassword = value
End Set
End Property
Public Property Email() As String
Get
Return strEmail
End Get
Set(ByVal value As String)
strEmail = value
End Set
End Property
Public Property accessLevel() As String
Get
Return shortAccessLevel
End Get
Set(ByVal value As String)
shortAccessLevel = value
End Set
End Property
Public Property status() As Byte
Get
Return byteStatus
End Get
Set(ByVal value As Byte)
byteStatus = value
End Set
End Property
End Class
Partial Class Login
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
btnLogin.Enabled = True
lblMsg.Text = ""
If Session("loginName") <> "" Then
btnLogin.Enabled = False
lblMsg.Text = "Please Logout first."
End If
txtName.Focus()
End Sub
Protected Sub btnlogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim objDBUser As New cDBUser
If objDBUser.checkLoginName(txtName.Text) = 1 Then
Dim accessLv As Integer = objDBUser.checkPassword(txtName.Text, txtPwd.Text)
If accessLv = -1 Then
lblMsg.Text = "Wrong Password. Please try again."
txtPwd.Focus()
ElseIf accessLv = 0 Then
lblMsg.Text = "Your account is locked. Contact with site admin."
Else
lblMsg.Text = "Login successfully."
Dim loginName As String = txtName.Text
Session.Add("loginName", loginName)
Session.Add("userName", objDBUser.getUserName(loginName))
Response.Redirect("Index.aspx")
End If
Else
lblMsg.Text = "Wrong Login Name."
txtName.Focus()
End If
End Sub
Protected Sub btnLogout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogout.Click
Session.Clear()
End Sub
End Class
这个是code behind file
另外需要在app code 里面放一个叫dbUser和Cuser的class
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Public Class cDBUser
Private conn As New SqlConnection
Private connstr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Private sqlCmd As New SqlCommand
Public Sub New()
conn.ConnectionString = connstr
sqlCmd.Connection = conn
End Sub
Public Function checkLoginName(ByVal loginName As String) As Integer
Try
conn.Open()
sqlCmd.CommandText = "SELECT COUNT(password) FROM tblUser where LoginName = '" & loginName & "'"
Return sqlCmd.ExecuteScalar
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function checkPassword(ByVal loginName As String, ByVal pwd As String) As Integer
Try
conn.Open()
sqlCmd.CommandText = "SELECT COUNT(*) FROM tblUser WHERE loginName = '" & loginName & _
"' and password = '" & pwd & "'"
Dim passwordOK As Integer = sqlCmd.ExecuteScalar
If passwordOK = 1 Then
sqlCmd.CommandText = "SELECT accesslevel FROM tblUser WHERE loginName = '" & loginName & _
"' and password = '" & pwd & "' and status = 1"
Return CInt(sqlCmd.ExecuteScalar)
Else
Return -1
End If
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function getOldPwd(ByVal loginName As String) As String
Try
conn.Open()
sqlCmd.CommandText = "SELECT Password FROM tblUser WHERE LoginName = '" & loginName & "'"
Return sqlCmd.ExecuteScalar
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
Finally
conn.Close()
End Try
End Function
Public Function getUserName(ByVal loginName As String) As String
Try
conn.Open()
sqlCmd.CommandText = "SELECT UserName FROM tblUser WHERE LoginName = '" & loginName & "'"
Return sqlCmd.ExecuteScalar
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
Finally
conn.Close()
End Try
End Function
Public Function getAllUsers() As DataView
Try
conn.Open()
Dim sqlAdpt As New SqlDataAdapter("SELECT userID,LoginName,UserName,Email,AccessLevel,Status FROM tblUser", connstr)
Dim dt As New DataTable
sqlAdpt.Fill(dt)
Return dt.DefaultView
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
Finally
conn.Close()
End Try
End Function
Public Function getUserDetails(ByVal loginName As String) As cUser
Try
conn.Open()
Dim sqlAdpt As New SqlDataAdapter("SELECT * from tblUser where loginName = '" & loginName & "'", connstr)
Dim dt As New DataTable
sqlAdpt.Fill(dt)
Dim dr As DataRow = dt.Rows(0)
Dim objUser As New cUser
objUser.userId = dr.Item("UserId")
objUser.loginName = dr.Item("LoginName")
objUser.password = dr.Item("Password")
objUser.userName = dr.Item("UserName")
objUser.Email = dr.Item("Email")
objUser.accessLevel = dr.Item("AccessLevel")
objUser.status = dr.Item("Status")
Return objUser
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
Finally
conn.Close()
End Try
End Function
Public Function changeUserStatus(ByVal userId As Integer) As Integer
Try
conn.Open()
sqlCmd.CommandText = "UPDATE tblUser SET status = (1 - status) WHERE userId = " & userId
Return CInt(sqlCmd.ExecuteScalar)
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function changeEmail(ByVal loginName As String, ByVal newEmail As String) As Integer
Try
conn.Open()
sqlCmd.CommandText = "UPDATE tblUser SET Email = '" & newEmail & "' WHERE LoginName = '" & loginName & "'"
Return sqlCmd.ExecuteNonQuery
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function changePwd(ByVal loginName As String, ByVal newPwd As String) As Integer
Try
conn.Open()
sqlCmd.CommandText = "UPDATE tblUser SET password = '" & newPwd & "' WHERE loginName = '" & loginName & "'"
Return sqlCmd.ExecuteNonQuery
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function addUser(ByVal objUser As cUser) As Integer
Try
conn.Open()
sqlCmd.CommandText = "INSERT INTO tblUser (userName,loginName,password,email,accessLevel,status) VALUES('" & _
Trim(objUser.userName) & "','" & Trim(objUser.loginName) & "','" & objUser.password & "','" & Trim(objUser.Email) & "','" & _
objUser.accessLevel & "',1)"
If sqlCmd.ExecuteNonQuery() = 1 Then
sendEmail()
Return 1
End If
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function retrievePwd(ByVal loginName As String) As Integer
Try
conn.Open()
sqlCmd.CommandText = "SELECT password from tblUser where loginName = '" & loginName & "'"
Dim pwd As String = sqlCmd.ExecuteScalar
Dim email As String = getEmail(loginName)
If email <> "" And pwd <> "" Then
sendEmail()
Return 1
Else
Return 0
End If
Catch ex As Exception
MsgBox(ex.Message)
Return 0
Finally
conn.Close()
End Try
End Function
Public Function getEmail(ByVal loginName As String) As String
conn.Close()
Try
conn.Open()
sqlCmd.CommandText = "SELECT Email from tblUser where loginName = '" & loginName & "'"
Dim email As String = sqlCmd.ExecuteScalar
Return email
Catch ex As Exception
MsgBox(ex.Message)
Return ""
Finally
conn.Close()
End Try
End Function
Private Sub sendEmail()
'some code here to send email
End Sub
End Class
Imports Microsoft.VisualBasic
Public Class cUser
Private intUserId As Integer
Private strUserName As String
Private strLoginName As String
Private strPassword As String
Private strEmail As String
Private shortAccessLevel As Short
Private byteStatus As Byte
Public Sub New()
End Sub
Public Property userId() As Integer
Get
Return intUserId
End Get
Set(ByVal value As Integer)
intUserId = value
End Set
End Property
Public Property userName() As String
Get
Return strUserName
End Get
Set(ByVal value As String)
strUserName = value
End Set
End Property
Public Property loginName() As String
Get
Return strLoginName
End Get
Set(ByVal value As String)
strLoginName = value
End Set
End Property
Public Property password() As String
Get
Return strPassword
End Get
Set(ByVal value As String)
strPassword = value
End Set
End Property
Public Property Email() As String
Get
Return strEmail
End Get
Set(ByVal value As String)
strEmail = value
End Set
End Property
Public Property accessLevel() As String
Get
Return shortAccessLevel
End Get
Set(ByVal value As String)
shortAccessLevel = value
End Set
End Property
Public Property status() As Byte
Get
Return byteStatus
End Get
Set(ByVal value As Byte)
byteStatus = value
End Set
End Property
End Class
展开全部
用c#写的,很简单,你自己改成VB吧
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 登录按钮单击事件
/// </summary>
protected void ButtonLogin_Click(Object sender, EventArgs e)
{
string connString = @"server=.;Initial Catalog=master;Integrated Security=True";
string sql = @"select * from Users where userId='"+ txtUserName.Text +"'";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();//打开连接
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
if (reader["userPwd"].ToString() == txtPassword.Text)//密码正确
{
Session["userID"] = txtUserName.Text.Trim();//存储用户名
Response.Redirect("AddressList.aspx");//进入系统
}
else
{
Response.Write("<script language=javascript>alert('密码错误!')</script>");
}
}
else
{
Response.Write("<script language=javascript>alert('用户名错误或者用户名不存在!')</script>");
}
reader.Close();
}
catch (Exception ee)
{
Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
}
finally
{
conn.Close();
}
}
}
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 登录按钮单击事件
/// </summary>
protected void ButtonLogin_Click(Object sender, EventArgs e)
{
string connString = @"server=.;Initial Catalog=master;Integrated Security=True";
string sql = @"select * from Users where userId='"+ txtUserName.Text +"'";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
conn.Open();//打开连接
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
if (reader["userPwd"].ToString() == txtPassword.Text)//密码正确
{
Session["userID"] = txtUserName.Text.Trim();//存储用户名
Response.Redirect("AddressList.aspx");//进入系统
}
else
{
Response.Write("<script language=javascript>alert('密码错误!')</script>");
}
}
else
{
Response.Write("<script language=javascript>alert('用户名错误或者用户名不存在!')</script>");
}
reader.Close();
}
catch (Exception ee)
{
Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
}
finally
{
conn.Close();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询