Need to add Windows Authentication to a VB application RRS feed

  • Question

  • I have a application that prints labels on a community computer.  I would like to add a Windows authentication login form to allow any of our employees to access a configuration screen.  The request would bring up the login form if the login passed (meaning the username / password was correct) the configuration form would open.  Otherwise the user would just get a warning Msgbox.  

    So far all of the examples I could find either apply ASP.NET or use the logged-in user. Not for additional access as I'm trying to do.

    Thank you for any help that can be given!

    Tuesday, June 28, 2011 7:43 PM


All replies

  • Hello DChiShaggy,

    Thanks for your post.

    Based on my understanding, you want to create a login form in VB.NET. If the username and password are entered correctly, then a configuration form will open. Otherwise, a warning messagebox will show.

    Please try below example which implements such a login form. If you have any concerns, please feel free to follow up.   

    Imports System.Data.SqlClient
    Public Class Form1
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Password.UseSystemPasswordChar = True
      End Sub
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim theConnectionString As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=db;Integrated Security=True")
        Dim theSqlCommand As New SqlCommand("SELECT [username] FROM [Table_2] WHERE [username] = @username AND [password] =@password", theConnectionString)
        Dim theUsername As New SqlParameter("@username", SqlDbType.NVarChar)
        theUsername.Value = Username.Text
        Dim thePassword As New SqlParameter("@password", SqlDbType.NVarChar)
        thePassword.Value = Password.Text
        'open the connection
        Dim theDataReader As SqlDataReader = theSqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
        Dim theLoginType As Object = 0
        If theDataReader.HasRows Then
          theLoginType = theDataReader(theLoginType)
        End If
        If theLoginType = Nothing Then
          MessageBox.Show("Sorry, username/password details were incorrect")
          MessageBox.Show("Login successful")
        End If
      End Sub
    End Class

    More information, you could also check this thread:

    Have a nice day.

    Best regards

    Liliane Teng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 29, 2011 6:35 AM
  • Windows Authentication Using Form Authentication


    Narendran Ponpandiyan

    Wednesday, June 29, 2011 11:43 AM
  • Whenever I use custom authentication against a domain I implement the LogonUser API function. You can find an example at the below link:

    How to validate Windows user rights in a Visual Basic .NET or Visual Basic 2005 application


    Paul ~~~~ Microsoft MVP (Visual Basic)
    • Marked as answer by DChiShaggy Thursday, June 30, 2011 12:24 PM
    Wednesday, June 29, 2011 1:52 PM
  • Sorry it has been a crazy morning, so I haven't been able to follow back up until now.

    Paul, I think your link is what I'm after.  I hope to spend some time tonight trying it out thanks.

    Wednesday, June 29, 2011 5:58 PM
  • Be aware that for this kind of solutions the user has to be a user who is running (one of) the desktop(s) of the computer.


    Thursday, June 30, 2011 5:42 AM
  • Paul that example worked almost perfect.  The only real change I had to make was to use our Domain instead of the Machine Name. 

    Thank you every one for your responses.


    Thursday, June 30, 2011 12:27 PM
  • Actually Cor, the user can be pretty much anyone. They just need to have credentials to log on to the domain (or local computer if desired). If believe the example uses the local computer, but a domain can be used as well.
    Paul ~~~~ Microsoft MVP (Visual Basic)
    Thursday, June 30, 2011 12:28 PM