How to use Windows Authentication

  • Thread starter Thread starter NetRacer
  • Start date Start date
N

NetRacer

hi,

i have a login window with username and password. if a user logs in, i want
to ask windows if on the current computer or in the domain there is a valid
user with the entered password. how can i do this?

thanx
netracer
 
Hi NetRacer,

You can get the windows identity and check it's IsAuthenticated property to
see if a user is authenticated.

Try this :

System.Security.Principal.WindowsIdentity winId =
System.Security.Principal.WindowsIdentity.GetCurrent();
if(winId.IsAuthenticated)
{
MessageBox.Show("I'm authenticated") ;
}

Good luck,
 
Dear NetRacer,
the code is FrameWork 2.0 compatible, it cannot be run in framework
1.1

Imports System
Imports System.Security
Imports System.Security.Principal
Imports System.Threading

Public Class WindowsIdentityCheck

' API's
Private Declare Auto Function LogonUser 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 Boolean

Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal
handle As IntPtr) As Boolean

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.

End Sub


Public Sub ValidateUserAccount(ByVal userName As String, ByVal
domainName As String, ByVal password As String)

Dim tokenHandle As New IntPtr(0)
Dim winPrin As WindowsPrincipal = Thread.CurrentPrincipal
Dim winId As WindowsIdentity = winPrin.Identity

Try

Const LOGON32_PROVIDER_DEFAULT As Integer = 0
'This parameter causes LogonUser to create a primary token.
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
'Const SecurityImpersonation As Integer = 2

tokenHandle = IntPtr.Zero

' Call LogonUser to obtain a handle to an access token.
Dim returnValue As Boolean = LogonUser(userName,
domainName, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, tokenHandle)

If False = returnValue Then

' The User Is Not A Valid User (Throw Exception Here)
Return
End If

' User Is Valid
CloseHandle(tokenHandle)
Catch e As Exception
End Try
End Sub
End Class



Regards,
Naveed Ahmad Bajwa
http://bajoo.blogspot.com/
 
Back
Top