PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Users unable to log-on - vb.net code-VBForums
Results 1 to 10 of 10

Thread: Users unable to log-on - vb.net code

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Users unable to log-on - vb.net code

    Hello

    I am able to register (register.aspx page) new users using the following code which populates 3 columns in my database: username, email, and hashed (for the password):

    Code:
    Private Sub CreateAccount(ByVal username As String, ByVal hashed As String, ByVal email As String)
    
            Dim hashedPassword As String = Crypto.HashPassword(hashed)
    
              Using connection As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
    
                Using cmd As New OleDbCommand("INSERT INTO university (username, strEmail, hashed) VALUES (@username, @strEmail, @hashed)", connection)
                    cmd.Parameters.AddWithValue("@username", username)
                    cmd.Parameters.AddWithValue("@strEmail", email)
                    cmd.Parameters.AddWithValue("@hashed", hashedPassword)
    
                    connection.Open()
                    cmd.ExecuteNonQuery()
                End Using
            End Using
    
        End Sub
    However, when one of those users tries to log-on (logon.aspx page), he gets a 'log-on denied' error. This is the code I have in logon.aspx.vb:

    Code:
    Private hashed As String
    
    CODE TO DISPLAY CURRENT DATE HERE
    
    Protected Sub LogonBtn_Click(sender As Object, e As EventArgs)
    
    Dim hashedPassword As String = Crypto.HashPassword(passwordTextBox.Text)
    'Authenticate user
    
    Dim Authenticated As Boolean = Authenticate(strEmailTextBox.Text, hashedPassword)
    'If authenticated, send user to userpage.aspx
    If Authenticated Then
                
    Dim target = String.Format("~/userpage.aspx?strEmailValue={0}", strEmailTextBox.Text)
    Session("strEmailValue") = strEmailTextBox.Text
    Response.Redirect(target, False)
    Else
    
    LabelError.Text = "Email/Password invalid. Login denied"
    LabelError.Visible = True
    End If
        
    End Sub
    
    Protected Function Authenticate(strEmailValue As String, hashedValue As String) As Boolean
    
    'strEmailValue is the unknown email variable        
    'hashedValue is the unknown password variable        
    'strEmailTextBox is the ID of the email textbox field in my aspx file        
    'passwordTextBox is the ID of the password textbox field in my aspx file        
    'strEmail is the name of the email column in my MS Access database
    
    Using connection As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
    
    Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = '" & strEmailValue & "' AND [hashed] = '" & hashedValue & "'"
    
    Dim cmd As New OleDbCommand(cmdText, connection)
    connection.Open()
                
    Dim result As Integer             
    connection.Close()
                
    Return result > 0
    End Using
        
    End Function
    What is wrong with this logon.aspx.vb code that prevents the user from verifying his password and logging on?

    Thanks.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,168

    Re: Users unable to log-on - vb.net code

    In Authenticate, your return value is based on the variable called result , which you never assign a value to.

    You also never execute the command object that you create... as a hint, ExecuteScalar would be a good idea.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Users unable to log-on - vb.net code

    Sorry, I don't seem to have copied the code faithfully.

    In the Validate function, I have:

    Code:
    Protected Function Authenticate(strEmailValue As String, hashedValue As String) As Boolean
    
            'strEmailValue is the unknown email variable
            'hashedValue is the unknown password variable
            'strEmailTextBox is the ID of the email textbox field in my aspx file
            'passwordTextBox is the ID of the password textbox field in my aspx file
            'strEmail is the name of the email column in my MS Access database
    
    Using connection As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
                
    Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = '" & strEmailValue & "' AND [hashed] = '" & hashedValue & "'"
    
    Dim cmd As New OleDbCommand(cmdText, connection)
    
    'Open and execute the command
    'Returns a count of the records found; zero means no records found while 1 and greater means records found
    
                connection.Open()
                Dim result As Integer = cmd.ExecuteScalar
                connection.Close()
                Return result > 0
    
            End Using
    
        End Function
    Thanks for replying!

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,981

    Re: Users unable to log-on - vb.net code

    It makes code rather easier to read if you use consistent indenting.
    vb.net Code:
    1. Protected Function Authenticate(strEmailValue As String, hashedValue As String) As Boolean
    2.     'strEmailValue is the unknown email variable
    3.     'hashedValue is the unknown password variable
    4.     'strEmailTextBox is the ID of the email textbox field in my aspx file
    5.     'passwordTextBox is the ID of the password textbox field in my aspx file
    6.     'strEmail is the name of the email column in my MS Access database
    7.  
    8.     Using connection As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings("students").ConnectionString)
    9.         Dim cmdText As String = "SELECT COUNT(strEmail) FROM university WHERE strEmail = '" & strEmailValue & "' AND [hashed] = '" & hashedValue & "'"
    10.         Dim cmd As New OleDbCommand(cmdText, connection)
    11.  
    12.         'Open and execute the command
    13.         'Returns a count of the records found; zero means no records found while 1 and greater means records found
    14.  
    15.         connection.Open()
    16.         Dim result As Integer = cmd.ExecuteScalar
    17.         connection.Close()
    18.  
    19.         Return result > 0
    20.     End Using
    21. End Function
    You should get that simply by copying from the IDE. If you don't have consistent indenting in the IDE, you should turn on Pretty Listing in the Options dialogue.

    Also note that you can copy an arbitrary rectangle of code by holding the Alt key while selecting. That means that you can copy code that is indented without retaining a fat wad of irrelevant whitespace to paste here.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,981

    Re: Users unable to log-on - vb.net code

    Debug your code. If ExecuteScalar is returning zero then that means that there are no records in your database that match the credentials you are providing. You need to actually look at those credentials with your own eyes as the code executes. For instance, are you sure that you are hashing the password they supply?

    Also, you really ought to be using parameters to insert values into your query. While sometimes it won't make a difference, using parameters will never hurt and not doing so can often have disastrous consequences.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Users unable to log-on - vb.net code

    Thanks for your reply.

    I haven't got that Pretty tool in my VS 2017 but, yes, there are online editors such as this one:

    http://www.aspindent.com/?indent

    I have placed a breakpoint next to

    Code:
    Dim result As Integer = cmd.ExecuteScalar
    and after pressing F5 to begin the Debugging, the output pane shows the following errors:

    Build started: Project: DimaWeb, Configuration: Debug Any CPU ------
    Validating Web Site
    Building directory '/Account/'.
    Building directory '/'.

    C:\Users\Steve\Documents\Visual Studio 2017\DimaWeb\DimaWeb\Account\RegisterExternalLogin.aspx.vb(2,0): warning BC40056: Namespace or type specified in the Imports 'DotNetOpenAuth.AspNet' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
    C:\Users\Steve\Documents\Visual Studio 2017\DimaWeb\DimaWeb\Account\RegisterExternalLogin.aspx.vb(78,0): error BC30002: Type 'AuthenticationResult' is not defined.
    Validation Complete
    ========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========
    In my Imports, I only have:

    Code:
    Imports System 'stores data specific to one user 
    Imports System.Data.OleDb
    Imports System.Linq
    Imports System.Web
    Imports System.Web.UI
    Imports System.Security.Cryptography
    Imports System.Text
    Imports System.Web.Helpers
    This tutorial identifies the first error (warning BC40056):

    https://support.microsoft.com/en-ph/...d-namespace-in

    In order to import a namespace you must first reference the assembly where the namespace resides. To reference the assembly, from the Project menu click Add Reference. On the .NET tab, select the assembly that contains the namespace that you are trying to import, or Browse to the folder that contains the assembly.
    I am guessing that Add Reference is that item under Website in VS 2017 (there is no tab called Project):

    Name:  ReferenceManager.jpg
Views: 82
Size:  32.4 KB

    What Namespace should I browse to, please? I didn't know DotNetOpenAuth.AspNet was the name of a Namespace.

    Thanks

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,981

    Re: Users unable to log-on - vb.net code

    Quote Originally Posted by SteveHi View Post
    I haven't got that Pretty tool in my VS 2017
    Of course you do. You're just one of the many people who hasn't bothered to open the Options dialogue and look through it to see what options you have.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Users unable to log-on - vb.net code

    Yes, sorry, I have found it:

    Name:  PrettyListing.jpg
Views: 83
Size:  30.2 KB

    I probably haven't kept to the original VS text formatting when I have posted the text here, thinking it was easier if it were all left-aligned, but now I know!

    What to do about that ExternalRegisterLogin and DotNetOpenAuth.AspNet issue

    uild started: Project: DimaWeb, Configuration: Debug Any CPU ------
    Validating Web Site
    Building directory '/Account/'.
    Building directory '/'.

    C:\Users\Steve\Documents\Visual Studio 2017\DimaWeb\DimaWeb\Account\RegisterExternalLogin.aspx.vb(2,0): warning BC40056: Namespace or type specified in the Imports 'DotNetOpenAuth.AspNet' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
    C:\Users\Steve\Documents\Visual Studio 2017\DimaWeb\DimaWeb\Account\RegisterExternalLogin.aspx.vb(78,0): error BC30002: Type 'AuthenticationResult' is not defined.
    Validation Complete
    that seems to be a problem. There may well be other problems with the code (Return result, etc) as you have previously mentioned, but I am trying to eliminate one problem at a time.

    Thank you again.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,981

    Re: Users unable to log-on - vb.net code

    It sounds like you're missing a NuGet package.

    https://www.nuget.org/packages/DotNetOpenAuth.AspNet

    By the way, I've never used OAuth and I just found that by copying "DotNetOpenAuth.AspNet" from your post into a search engine.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Users unable to log-on - vb.net code

    Many thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width