请教ASP.NET表单-数据库身份验证的例子,最好是VB.NET的

KOON 2004-03-23 10:16:34
在微软的快速入门中看了半天也没有看懂,实在不明白没有POST的验证是如何实现的?请问有没有朋友能给这方面的例子,就是我有一个登录页面,上面有用户名和密码的表单,我提交这个表单和数据库中的用户密码相验证,成功即解除web.config的验证限制,否则退回logon page,谢谢
...全文
98 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhy0101 2004-03-27
  • 打赏
  • 举报
回复
这里只贴了代码部分,网页就不贴了
Imports System.Web.Security
Imports System.Data.OleDb
Public Class login
Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents txtAcc As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPwd As System.Web.UI.WebControls.TextBox
Protected WithEvents btnLog As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents chkAdmin As System.Web.UI.WebControls.CheckBox
Protected WithEvents RequiredFieldValidatorAcc As System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents RequiredFieldValidatorPwd As System.Web.UI.WebControls.RequiredFieldValidator

'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If User.Identity.IsAuthenticated Then
Response.Redirect("logoff.aspx")
End If
End Sub

Private Sub btnLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLog.Click

Dim strUsername As String
Dim strPassword As String
Dim strAuthor As String

strUsername = txtAcc.Text.ToUpper
strPassword = txtPwd.Text
If chkAdmin.Checked = False Then
strAuthor = "Student"
Else
strAuthor = "Admin"
End If
If Authenticate(strUsername, strPassword, strAuthor) Then
FormsAuthentication.RedirectFromLoginPage(strUsername, False)
End If
End Sub
Private Function Authenticate(ByVal strUsername As String, ByVal strPassword As String, ByVal strAuthor As String) As Boolean
Dim strQuery As String
Dim blResult As Boolean

strQuery = "SELECT * FROM Yonghu WHERE Kaohao='" & strUsername & "' AND Mima='" & strPassword & "' AND Quanxian='" & strAuthor & "'"
Dim conExam As OleDbConnection
Dim cmdSelect As OleDbCommand

blResult = False

conExam = New OleDbConnection(strConn)
cmdSelect = New OleDbCommand(strQuery, conExam)
Try
conExam.Open()
blResult = cmdSelect.ExecuteReader.Read
Finally
conExam.Close()
End Try
Return blResult
End Function
End Class
CMFUKING 2004-03-27
  • 打赏
  • 举报
回复
<!--
The LoginBtn_Click event handler uses the Authenticate method to check the
user name and password entered by the user against the credentials stored
in the credential data store. If the username and password are correct as
verified by the authentication event handler function, the RedirectFromLoginPage
method transfers the user back to the protected resource. The
FormsAuthenticationEventHandler Delegate function is not included in this example.
-->
<%@ Page Language="VB" AutoEventWireup="true" %>
<html>
<head>
<script runat="server">
Sub LoginBtn_Click(sender as Object, e as EventArgs)
If Page.IsValid Then
' Call the authentication event handler delegate (not included in this example).
If FormsAuthentication.Authenticate(UserName.Text, UserPass.Text) Then
' Return to the originally requested URL.
FormsAuthentication.RedirectFromLoginPage(UserName.Text, Remember.Checked)
Else
Msg.Text = "Invalid Credentials: Please try again"
End If
End If
End Sub
</script>

</head>

<body>
<!--
This form gets the user name and password from the user.
The LoginBtn_Click event handler is called when the
Login button is clicked to authenticate the user.
-->
<form runat="server">
<h2>Login Page</h2>
<hr size="1" />
<table>
<tbody>
<tr>
<td>Username:</td>
<td><asp:TextBox id="UserName" runat="server"></asp:TextBox></td>
<td><asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="UserName"
Display="Static" ErrorMessage="*"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>Password:</td>
<td><asp:TextBox id="UserPass" runat="server" TextMode="Password"></asp:TextBox></td>
<td><asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ControlToValidate="UserPass"
Display="Static" ErrorMessage="*"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td colspan="3"> <asp:CheckBox id="Remember" runat="server"></asp:CheckBox>Remember credentials?</td>
</tr>
</tbody>
</table>
<asp:button id="LoginBtn" onclick="LoginBtn_Click" runat="server" text="Login"></asp:button>
<p><asp:Label id="Msg" runat="server" ForeColor="red"></asp:Label></p>
</form>
</body>
</html>
ghost225 2004-03-23
  • 打赏
  • 举报
回复
很简单啊 ,修改web。config里的内容就行了呀
KOON 2004-03-23
  • 打赏
  • 举报
回复
UP

62,046

社区成员

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

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

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

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