Use windows credentials in a VB 2005 login form for user authentication.

Posted on 2007-08-02
Last Modified: 2012-06-27
Hello.Im building an application in visual studio 2005 and I want my users in the login section to use their windows account name and password for authentication.I have two text boxes for the username and password and a button which I want to contain authentincation code that will read the credentials from windows and compare it with the user input.I dont know how to proceed.Any ideas?
Question by:Spyros_Savvides
    LVL 53

    Accepted Solution

    You can get the username like this:

    Private Sub GetUserInfo()Dim UserIdentityInfo As System.Security.Principal.WindowsIdentityDim strMsg As String
    UserIdentityInfo = System.Security.Principal.WindowsIdentity.GetCurrent()

    strMsg = "User Name: " & UserIdentityInfo.Name & vbCrLf
    strMsg = strMsg & " Token: " & UserIdentityInfo.Token.ToString() & vbCrLf
    strMsg = strMsg & " Authenticated: " & UserIdentityInfo.AuthenticationType & vbCrLf
    strMsg = strMsg & " System: " & UserIdentityInfo.IsSystem & vbCrLf
    strMsg = strMsg & " Guest: " & UserIdentityInfo.IsGuest & vbCrLf
    strMsg = strMsg & " Anonymous: " & UserIdentityInfo.IsAnonymous & vbCrLf

    End Sub

    It's impossible to read the windows password !

    Author Comment

    Thank you very much sir.Your solution was very helpful but I think that it is possible to read the password.There is an application called videoguard that authenticates the user only with the windows account name and password.if you want you can check it out.
    LVL 53

    Expert Comment

    You can't read it, but propably the program uses some kind of authentication against the active directory. (like

    Authenticate a user against the Active Directory

    Author Comment

    I will check it out.I have one more question.When I compare the strMessage string with my textbox input I cannot have a succesfull authentication.My code is as follows:

    Private Sub Applybt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Applybt.Click
            Dim username As String
            Dim password As String
            Dim pass_confirm As String
            Dim iTotalTabs = Me.TabControl.TabCount()
            Dim X As Integer
            Dim UserIdentityInfo As System.Security.Principal.WindowsIdentity

            UserIdentityInfo = System.Security.Principal.WindowsIdentity.GetCurrent()

     username = UserIdentityInfo.Name & vbCrLf
            password = "1234"
            pass_confirm = "1234"

            If (username = usernametxt.Text) And (password = passwordtxt.Text) And (pass_confirm = confirmpasswordtxt.Text) Then
                Applybt.Enabled = False
                MsgBox("Authentication successfull.")
                LogOffbt.Enabled = True

                For X = 1 To iTotalTabs - 1
                    With Me.TabControl.TabPages(X)
                        .Enabled = True
                    End With

            ElseIf (usernametxt.Text = "") And (passwordtxt.Text = "") Then
                MsgBox("You must enter a username and a  password.")

            ElseIf (usernametxt.Text = "") Then
                MsgBox("Please enter a username.")

            ElseIf (passwordtxt.Text = "") Then
                MsgBox("Please enter a password.")

            ElseIf (confirmpasswordtxt.Text = "") Then
                MsgBox("You must confirm your password.")
            ElseIf (passwordtxt.Text <> confirmpasswordtxt.Text) Then
                MsgBox("Password and confirmation of password must be the same.")

            Else : MsgBox("Authentication unsuccesfull.Please check your username and password.")

            End If

               End Sub

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now