Results 1 to 3 of 3

Thread: Windows Authentication.

  1. #1

    Thread Starter
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Windows Authentication.

    Hi,
    use this code to authenticate a user on a windows domain.

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

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

  2. #2
    New Member
    Join Date
    Jun 2005
    Posts
    4

    Re: Windows Authentication.

    Hi
    Didn't work in my system.
    Wonder what went wrong.

  3. #3
    New Member kingkoder's Avatar
    Join Date
    Jan 2009
    Location
    The Great North
    Posts
    11

    Re: Windows Authentication.

    Hi, using windows authentication is there any way to grant permissions for certain users to access a form and not others? And is it possible that they can only view certain ones but edit others?
    relentless...

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