Results 1 to 12 of 12

Thread: Authenticate users

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    416

    Authenticate users

    Hi,

    How can I authenticate a Windows domain user a/c using vb.net?
    User will input password, and this function will authenticate it against the NT domain.

    Thx!

  2. #2
    Member
    Join Date
    Jan 2005
    Location
    in the nederlands
    Posts
    37

    Talking Re: Authenticate users

    have you 2 textboxes ?

  3. #3
    Member
    Join Date
    Jan 2005
    Location
    in the nederlands
    Posts
    37

    Thumbs up Re: Authenticate users

    if you have 2 textboxes use this script then

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         If TextBox1.Text = "killerbeam2" and textbox2 = "rre" Then
    3.             MsgBox("password compleet", "do you realy wand use the script page ?", MsgBoxStyle.YesNo)
    4.         ElseIf TextBox1.Text = "killerbeam3" and textbox2 = "rre" Then
    5.             MsgBox("password compleet", "do you realy wand enter alfra glas windows?", MsgBoxStyle.YesNoCancel)
    6.         ElseIf TextBox1.Text = "killerbeam4" and textbox2 = "rre" Then
    7.             MsgBox("password compleet", "do you realy wand to use the database ?", MsgBoxStyle.YesNo)
    8.         Else
    9.             MsgBox("sorry bad password try it later again !")
    10.             timer1.enabled = true
    11.         End If
    12.     End Sub


  4. #4
    Member
    Join Date
    Jan 2005
    Location
    in the nederlands
    Posts
    37

    Thumbs up Re: Authenticate users

    or if you wand to use a database have i this script for you

    VB Code:
    1. Imports System.Data.OleDb

    VB Code:
    1. Dim mypath = Application.StartupPath & "\mydb.mdb"
    2.     Dim mypassword = ""
    3.     Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mypath & ";Jet OLEDB:Database Password=" & mypassword)
    4.     Dim cmd As OleDbCommand

    VB Code:
    1. Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    2.         Dim sql = "SELECT UserID ,PassID FROM MYTAB WHERE USERID='" & TextBox2.Text & "' AND PASSID='" & TextBox3.Text & "'"
    3.  
    4.         cmd = New OleDbCommand(sql, conn)
    5.         conn.Open()
    6.         Dim dr As OleDbDataReader = cmd.ExecuteReader
    7.  
    8.         Try
    9.             If dr.Read = False Then
    10.                 MessageBox.Show("Authentication failed...")
    11.                 End
    12.             Else
    13.                 MessageBox.Show("Login successfully...")
    14.             End If
    15.         Catch ex As Exception
    16.             MsgBox(ex.Message)
    17.         End Try
    18.  
    19.         TextBox3.Text = ""
    20.         Label13.Text = TextBox2.Text
    21.         Label14.Text = "tell not !!!"
    22.  
    23.  
    24.     End Sub


  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    416

    Re: Authenticate users

    thx for ur reply, but this is not what I want.
    I need to authenticate the password of a domain a/c.
    i.e. the a/c u use to logon the NT domain (not local a/c)....any idea?

  6. #6
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Authenticate users

    Hi stm
    I use this piece of code on my frmlogin. On the button1_click event me.textbox1.text contains the user name, me.textbox2.text the password, and 'dinefer' is my windows domain name.

    VB Code:
    1. Imports System.Security.Principal
    2. Imports System.Runtime.InteropServices
    3.  
    4.  
    5.  
    6.     Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As Integer, ByVal dwLogonProvider As Integer, ByRef phToken As IntPtr) As Integer
    7.     Declare Auto Function DuplicateToken Lib "advapi32.dll" (ByVal ExistingTokenHandle As IntPtr, ByVal ImpersonationLevel As Integer, ByRef DuplicateTokenHandle As IntPtr) As Integer
    8.     Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
    9.     Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
    10.     Private LOGON32_LOGON_INTERACTIVE As Integer = 2
    11.     Private LOGON32_PROVIDER_DEFAULT As Integer = 0
    12.     Private impersonationContext As WindowsImpersonationContext
    13.  
    14.     Private Function impersonateValidUser(ByVal userName As String, ByVal domain As String, ByVal password As String) As Boolean
    15.  
    16.         Dim tempWindowsIdentity As WindowsIdentity
    17.         Dim token As IntPtr = IntPtr.Zero
    18.         Dim tokenDuplicate As IntPtr = IntPtr.Zero
    19.         impersonateValidUser = False
    20.  
    21.         If RevertToSelf() Then
    22.         If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
    23.                 If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
    24.                     tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
    25.                     impersonationContext = tempWindowsIdentity.Impersonate()
    26.                     If Not impersonationContext Is Nothing Then
    27.                         impersonateValidUser = True
    28.                     End If
    29.                 End If
    30.             End If
    31.         End If
    32.         If Not tokenDuplicate.Equals(IntPtr.Zero) Then
    33.             CloseHandle(tokenDuplicate)
    34.         End If
    35.         If Not token.Equals(IntPtr.Zero) Then
    36.             CloseHandle(token)
    37.         End If
    38.     End Function
    39.     Private Sub undoImpersonation()
    40.         impersonationContext.Undo()
    41.     End Sub
    42.  
    43.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    44.         If impersonateValidUser(Me.TextBox1.Text, "dinefer", Me.TextBox2.Text) Then
    45.             MessageBox.Show("User authenticated sucessfully")
    46.             'Insert your code that runs under the security context of a specific user here.
    47.             undoImpersonation()
    48.         Else
    49.             MessageBox.Show("Invalid password or user")
    50.             'Your impersonation failed. Therefore, include a fail-safe mechanism here.
    51.         End If
    52.  
    53.     End Sub

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  7. #7
    Member
    Join Date
    Jan 2005
    Location
    in the nederlands
    Posts
    37

    Talking Re: Authenticate users

    ok sorry then that i not can help you
    what is xml ???

    help my please on this link for xml
    http://www.vbforums.com/showthread.php?t=322815



  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    416

    Re: Authenticate users

    Hi Asgorath,

    Need my user have any special privileges in order to run this code?
    They just authenticate themselves.....

    Thx!

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    416

    Re: Authenticate users

    Hi,

    One more question...how can I get the domain and userID of current logon user?

    Thx!

  10. #10
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Authenticate users

    Hi
    No the user doesn't any special priviledges to run the code, the user must be a domain user that's all.

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  11. #11
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Authenticate users

    Hi
    The domain name is
    VB Code:
    1. SystemInformation.UserDomainName()
    The UserID is
    VB Code:
    1. System.Environment.UserName

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    416

    Re: Authenticate users

    Hi,

    Thx a lot!

Posting Permissions

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



Click Here to Expand Forum to Full Width