vb.net登录控件未重新验证

vb.net登录控件未重新验证,vb.net,authentication,login-control,Vb.net,Authentication,Login Control,当涉及到vb.net登录控件时,我是新手,所以请容忍我 Tp start我正在使用ASP.NET4.0和vb.net 好的,我有一个简单的登录控件,它根据sql数据库验证用户。(我使用hostgator托管,因此无法使用正常的windows身份验证)。现在,我遇到的最大问题是,如果会话超时,您被重定向到登录页面,那么您在登录表单中键入的用户名/密码是什么并不重要,它只允许您在用户名和密码错误或用户不存在的情况下正确输入 如何确保登录控件真正对用户进行身份验证 非常感谢您的帮助。 谢谢 实际上。。

当涉及到vb.net登录控件时,我是新手,所以请容忍我

Tp start我正在使用ASP.NET4.0和vb.net

好的,我有一个简单的登录控件,它根据sql数据库验证用户。(我使用hostgator托管,因此无法使用正常的windows身份验证)。现在,我遇到的最大问题是,如果会话超时,您被重定向到登录页面,那么您在登录表单中键入的用户名/密码是什么并不重要,它只允许您在用户名和密码错误或用户不存在的情况下正确输入

如何确保登录控件真正对用户进行身份验证

非常感谢您的帮助。 谢谢


实际上。。此问题可能来自您对FormsAuthentication.RedirectFromLoginPage.的调用。。不过我还是冒昧地清理了一下你的代码。我还在身份验证方法中添加了FormsAuthentication.SetAuthCookie。。该cookie的名称和持续时间将在web.config文件中配置。。或您的“配置设置”

除非您愿意继承、清除和替换ASP.NET默认FormAuthenticationModule。。您将不得不部分依赖web.config配置设置

Public strLoginErrorMsg As String
Public type As String
Public rowcount As String

Protected Sub login_sbts_Authenticate(sender As Object, e As      System.Web.UI.WebControls.AuthenticateEventArgs) Handles login_sbts.Authenticate
    If isValidUser(login_sbts.UserName, login_sbts.Password) Then
        e.Authenticated = True
        FormsAuthentication.SetAuthCookie(login_sbts.UserName, false, "/")    
        lblInfo.Text = type

        If type = "ADMIN" Then
            Response.Redirect("dailynote.aspx")
        Else
            FormsAuthentication.RedirectFromLoginPage(Me.login_sbts.UserName, True)
            'Response.Redirect("other.aspx")
        End If
    Else
        e.Authenticated = false
    End If
End Sub

Private Function isValidUser(ByVal username As String, ByVal pwd As String) As Boolean
    isValidUser = False
    Dim conn As New SqlConnection("Data Source=localhost;Initial Catalog=sbts-scheduling;User ID=userid;Password=password;")
    Dim cmd As New SqlCommand("select * from tblusers where UserName='" & username & "' and Password='" & pwd & "'", conn)
    Using conn
        conn.open
        Using reader As system.data.sqlclient.SqlDataReader = comm.ExecuteReader
           If reader.Count > 0 Then
                'Not Checking for multible records here.
                While reader.read
                    If Not( IsDBNull(reader("UserType")) Then
                        Session("usertype") = reader("UserType").Trim()
                        IsValidUser = True
                    End If
                End While
            End If
        End Using
        If Not( conn.State = State.Close) Then
            conn.Close
        End If
    End Using
End Function

Protected Sub login_sbts_LoginError(sender As Object, e As System.EventArgs) Handles login_sbts.LoginError
    login_sbts.FailureText = strLoginErrorMsg
End Sub

我建议您考虑继承MembershipProvider。它使使用asp服务器标记变得更容易,因为您只需在标记的属性中指定提供者。(在web.config、app.config.中引用并正确配置它之后,或通过IIS(需要放置在全局缓存程序集和所有其他循环中,才能成为受信任的提供程序。)

您可能也应该在此处发布web.config文件的相关部分。system.web、身份验证和授权在这一部分中,包括您的首页代码,其中应包含您的asp:login部分,或asp:logincontrol..您正在使用的部分。顺便说一句..您在此处存在SQLInjection漏洞,我在m中没有提到y回答代码示例。您应该使用StoreProcedures,并使用新的SqlClient.Data.SqlParameters(@param,”)将这些值传递给存储过程(在您的SqlCommand.Parameter集合中),在分配SqlCommand.CommandType=CommandType.StoredProcedure.Savyconn.state时,它是一个枚举..并且可能是“ConnectionState.Close”或其他东西..需要一秒钟的时间来查找..但是..呃。。
Public strLoginErrorMsg As String
Public type As String
Public rowcount As String

Protected Sub login_sbts_Authenticate(sender As Object, e As      System.Web.UI.WebControls.AuthenticateEventArgs) Handles login_sbts.Authenticate
    If isValidUser(login_sbts.UserName, login_sbts.Password) Then
        e.Authenticated = True
        FormsAuthentication.SetAuthCookie(login_sbts.UserName, false, "/")    
        lblInfo.Text = type

        If type = "ADMIN" Then
            Response.Redirect("dailynote.aspx")
        Else
            FormsAuthentication.RedirectFromLoginPage(Me.login_sbts.UserName, True)
            'Response.Redirect("other.aspx")
        End If
    Else
        e.Authenticated = false
    End If
End Sub

Private Function isValidUser(ByVal username As String, ByVal pwd As String) As Boolean
    isValidUser = False
    Dim conn As New SqlConnection("Data Source=localhost;Initial Catalog=sbts-scheduling;User ID=userid;Password=password;")
    Dim cmd As New SqlCommand("select * from tblusers where UserName='" & username & "' and Password='" & pwd & "'", conn)
    Using conn
        conn.open
        Using reader As system.data.sqlclient.SqlDataReader = comm.ExecuteReader
           If reader.Count > 0 Then
                'Not Checking for multible records here.
                While reader.read
                    If Not( IsDBNull(reader("UserType")) Then
                        Session("usertype") = reader("UserType").Trim()
                        IsValidUser = True
                    End If
                End While
            End If
        End Using
        If Not( conn.State = State.Close) Then
            conn.Close
        End If
    End Using
End Function

Protected Sub login_sbts_LoginError(sender As Object, e As System.EventArgs) Handles login_sbts.LoginError
    login_sbts.FailureText = strLoginErrorMsg
End Sub