473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can some please explain about code regarding authentication

Hello, I was wondering if someone can help me understand something. Below is
some code that I got from the MS website that is suppose to authenticate for
the username and password on the local machine. I tested this code in VB.net
2003 in a WINDOW FORM class and it seems to work fine. However, when I test
it in VB.net 2005 Express Edition it works for the login portion but it
doesn't display the error message if the user name and password is incorrect,
it doesn't do anything:

Imports System.Security.Principal
Imports System.Security.Permissions
Imports System.Runtime.InteropServices
Imports System.Environment

'The LogonUser function tries to log on to the local computer
'by using the specified user name. The function authenticates
'the Windows user with the password provided.
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

'The FormatMessage function formats a message string that is passed as
input.
<DllImport("kernel32.dll")> _
Public Shared Function FormatMessage(ByVal dwFlags As Integer, ByRef
lpSource As IntPtr, _
ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByRef
lpBuffer As [String], _
ByVal nSize As Integer, ByRef Arguments As IntPtr) As Integer
End Function

'The CloseHandle function closes the handle to an open object such as an
Access token.
Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal
handle As IntPtr) As Boolean

'The GetErrorMessage function formats and then returns an error message
'that corresponds to the input error code.
Public Shared Function GetErrorMessage(ByVal errorCode As Integer) As
String
Dim FORMAT_MESSAGE_ALLOCATE_BUFFER As Integer = &H100
Dim FORMAT_MESSAGE_IGNORE_INSERTS As Integer = &H200
Dim FORMAT_MESSAGE_FROM_SYSTEM As Integer = &H1000

Dim msgSize As Integer = 255
Dim lpMsgBuf As String
Dim dwFlags As Integer = FORMAT_MESSAGE_ALLOCATE_BUFFER Or
FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS

Dim lpSource As IntPtr = IntPtr.Zero
Dim lpArguments As IntPtr = IntPtr.Zero
'Call the FormatMessage function to format the message.
Dim returnVal As Integer = FormatMessage(dwFlags, lpSource,
errorCode, 0, lpMsgBuf, _
msgSize, lpArguments)
If returnVal = 0 Then
Throw New Exception("Failed to format message for error code " +
errorCode.ToString() + ". ")
End If
Return lpMsgBuf
End Function

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim tokenHandle As New IntPtr(0)
Try

Dim UserName, MachineName, Pwd As String
'The MachineName property gets the name of your computer.
MachineName = System.Environment.MachineName
UserName = TextBox1.Text
Pwd = TextBox2.Text
Dim frm2 As New Form2
Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
tokenHandle = IntPtr.Zero
'Call the LogonUser function to obtain a handle to an access
token.
Dim returnValue As Boolean = LogonUser(UserName, MachineName,
Pwd, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, tokenHandle)

If returnValue = False Then
'This function returns the error code that the last
unmanaged function returned.
Dim ret As Integer = Marshal.GetLastWin32Error()
Dim errmsg As String = GetErrorMessage(ret)
frm2.Show()
frm2.Label1.Text = errmsg
frm2.Button1.Visible = False
Else
'Create the WindowsIdentity object for the Windows user
account that is
'represented by the tokenHandle token.
Dim newId As New WindowsIdentity(tokenHandle)
Dim userperm As New WindowsPrincipal(newId)
'Verify whether the Windows user has administrative
credentials.
If userperm.IsInRole(WindowsBuiltInRole.Administrator ) Then
frm2.Button1.Text = "Add Numbers"
frm2.Label1.Text = "Click this button to add two numbers"
frm2.Show()
Else
frm2.Label1.Text = " You do not have administrative
credentials."
frm2.Button1.Visible = False
frm2.Show()
End If
End If

'Free the access token.
If Not System.IntPtr.op_Equality(tokenHandle, IntPtr.Zero) Then
CloseHandle(tokenHandle)
End If
Catch ex As Exception
MessageBox.Show("Exception occurred. " + ex.Message)
End Try
End Sub

--
TC
Feb 1 '06 #1
1 1291
Forget that I asked this question. I'm a freaking idiot. I figured out what
the problem was.
--
TC
"Terrance" wrote:
Hello, I was wondering if someone can help me understand something. Below is
some code that I got from the MS website that is suppose to authenticate for
the username and password on the local machine. I tested this code in VB.net
2003 in a WINDOW FORM class and it seems to work fine. However, when I test
it in VB.net 2005 Express Edition it works for the login portion but it
doesn't display the error message if the user name and password is incorrect,
it doesn't do anything:

Imports System.Security.Principal
Imports System.Security.Permissions
Imports System.Runtime.InteropServices
Imports System.Environment

'The LogonUser function tries to log on to the local computer
'by using the specified user name. The function authenticates
'the Windows user with the password provided.
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

'The FormatMessage function formats a message string that is passed as
input.
<DllImport("kernel32.dll")> _
Public Shared Function FormatMessage(ByVal dwFlags As Integer, ByRef
lpSource As IntPtr, _
ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByRef
lpBuffer As [String], _
ByVal nSize As Integer, ByRef Arguments As IntPtr) As Integer
End Function

'The CloseHandle function closes the handle to an open object such as an
Access token.
Public Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal
handle As IntPtr) As Boolean

'The GetErrorMessage function formats and then returns an error message
'that corresponds to the input error code.
Public Shared Function GetErrorMessage(ByVal errorCode As Integer) As
String
Dim FORMAT_MESSAGE_ALLOCATE_BUFFER As Integer = &H100
Dim FORMAT_MESSAGE_IGNORE_INSERTS As Integer = &H200
Dim FORMAT_MESSAGE_FROM_SYSTEM As Integer = &H1000

Dim msgSize As Integer = 255
Dim lpMsgBuf As String
Dim dwFlags As Integer = FORMAT_MESSAGE_ALLOCATE_BUFFER Or
FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS

Dim lpSource As IntPtr = IntPtr.Zero
Dim lpArguments As IntPtr = IntPtr.Zero
'Call the FormatMessage function to format the message.
Dim returnVal As Integer = FormatMessage(dwFlags, lpSource,
errorCode, 0, lpMsgBuf, _
msgSize, lpArguments)
If returnVal = 0 Then
Throw New Exception("Failed to format message for error code " +
errorCode.ToString() + ". ")
End If
Return lpMsgBuf
End Function

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim tokenHandle As New IntPtr(0)
Try

Dim UserName, MachineName, Pwd As String
'The MachineName property gets the name of your computer.
MachineName = System.Environment.MachineName
UserName = TextBox1.Text
Pwd = TextBox2.Text
Dim frm2 As New Form2
Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
tokenHandle = IntPtr.Zero
'Call the LogonUser function to obtain a handle to an access
token.
Dim returnValue As Boolean = LogonUser(UserName, MachineName,
Pwd, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, tokenHandle)

If returnValue = False Then
'This function returns the error code that the last
unmanaged function returned.
Dim ret As Integer = Marshal.GetLastWin32Error()
Dim errmsg As String = GetErrorMessage(ret)
frm2.Show()
frm2.Label1.Text = errmsg
frm2.Button1.Visible = False
Else
'Create the WindowsIdentity object for the Windows user
account that is
'represented by the tokenHandle token.
Dim newId As New WindowsIdentity(tokenHandle)
Dim userperm As New WindowsPrincipal(newId)
'Verify whether the Windows user has administrative
credentials.
If userperm.IsInRole(WindowsBuiltInRole.Administrator ) Then
frm2.Button1.Text = "Add Numbers"
frm2.Label1.Text = "Click this button to add two numbers"
frm2.Show()
Else
frm2.Label1.Text = " You do not have administrative
credentials."
frm2.Button1.Visible = False
frm2.Show()
End If
End If

'Free the access token.
If Not System.IntPtr.op_Equality(tokenHandle, IntPtr.Zero) Then
CloseHandle(tokenHandle)
End If
Catch ex As Exception
MessageBox.Show("Exception occurred. " + ex.Message)
End Try
End Sub

--
TC

Feb 1 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kris van der Mast | last post by:
Hi, I've created a little site for my sports club. In the root folder there are pages that are viewable by every anonymous user but at a certain subfolder my administration pages should be...
2
by: J-T | last post by:
Hi All, We have an asp.net application on a windows 2003 server which is part of a domain controller which my worksatis is too. We have impersonated in our applciation with a fixed identity...
3
by: Alan Silver | last post by:
Hello, I have some pages that are protected by forms authentication, and am adding code to the global.asax so that if someone tries to load (say) /order83.aspx, if they are logged in, it will...
7
by: Steven Blair | last post by:
I have a DB connection performance issue in my C# app. I have used the Stopwatch to track how long it takes to do a couple things. Creating a SqlConnection object takes 19ms, and Opening a...
1
by: teddymeu | last post by:
hi guys I posted the other day regarding a solution i needed to design, im new to development an asp,net and built a local post office search tool using asp.net 2. vb in visual studio, using an...
4
by: Brad Isaacs | last post by:
I am working with ASP.NET 2.0 and using an SQL Server 2000 database. I am using Visual Studio 2005 and developing on my Local machine. I am working with Login controls ASP.Configuration, I...
2
by: Annie | last post by:
Hello guys, I have set the MEMBERSHIP, ROLEMANAGER and PROFILE in my config file as below. I just want to use my own sql server 2000 table instead of MSDB.
2
by: mark4asp | last post by:
This is a simplified version of my site. There are Premium users who have access to the Premium directory. Anyone else attempting to access it should be logged and then redirected to the...
2
by: bhargavigupta | last post by:
hi my application is login page with windows authentication i got the current user name but i am not getting password how can i get from the currently login user and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.