本文讨论了如何验证用户的 Windows 用户名称和 Microsoft Visual Basic.net 或 Microsoft Visual Basic 2005 应用程序中的密码。本文包括一个代码示例验证一个 Visual Basic.net 或 Visual Basic 2005 的应用程序中的用户。
Visual Basic.net 或 Visual Basic 2005 应用程序将验证用户的 Windows 用户名和密码后,应用程序还将验证 Windows 用户具有管理凭据。应用程序通过执行下列执行验证:
- 应用程序传递用户名和密码以 LogonUser 函数。
- LogonUser 函数验证用户名和密码,如果是有效的用户名和密码,则 返回 true
- 用户名和密码有效,LogonUser 函数接收一个句柄为令牌表示的 Windows 用户。
- WindowsIdentity 对象使用此标记来表示 Windows 用户在应用程序。
- WindowsPrincipal 对象使用 WindowsIdentity 对象验证 Windows 用户具有管理凭据。
- 如果 Windows 用户具有管理凭据的 ,应用程序允许 Windows 用户继续。如果 Windows 用户不具有管理凭据,Windows 用户无法继续,必须退出 Visual Basic.net 应用程序。
本分步指南介绍了如何验证用户的 Windows 用户名和密码在通过使用 Visual Basic.net 创建的应用程序中。本文还介绍了如何向基于 windows 中已被授予该用户权限的 Windows 用户提供功能的用户帐户。
要求
本文假定您熟悉下列主题:
- Windows 应用程序
- 可视的基本.net 或 Visual Basic 2005 编程
下面的列表概述了建议的硬件、 软件、 网络的基础结构和服务包,您需要:
- Microsoft Visual Studio.net 或 Microsoft Visual Studio 2005
- microsoft.net 框架
- Windows 平台软件开发工具包 (SDK)
创建一个 Visual Basic.net 或 Visual Basic 2005 应用程序
若要进行一个应用程序验证用户名和密码,请按照下列步骤操作:
- 启动 Visual Studio.net 或 Visual Studio 2005。
- 在 文件 菜单上指向 新建,然后单击 项目。在 新建项目 对话框。
- 在 项目类型 框中,单击 Visual Basic 项目。
注意在 Visual 有关 Studio 的 2005 年单击 项目类型 下的 Visual Basic。 - 在 模板 框中,单击 Windows 应用程序。
- 在 名称 框中键入 MyApp,然后单击 确定。默认状态下,创建名为 Form1 的 Windows 窗体。
设计 Windows 窗体来实现验证
您可以设计 Windows 窗体接受用户名和密码在运行时通过使用
文本框 控件。然后,您可以进行验证在 Windows 应用程序用户的用户权限时 Windows 用户单击一个
按钮 控件。若要这样做,请按照下列步骤操作:
- 在 视图 菜单上单击 工具箱。
- 在工具箱双击 TextBox 控制要将两个 文本框 控件添加到 Form1 窗体的两倍。默认状态下,TextBox1TextBox 控件和 TextBox2TextBox 控件添加到 Form1 窗体上。
- 位置 TextBox 控件以便 TextBox2TextBox 控件低于 TextBox1TextBox 控件并为 TextBox1TextBox 控件垂直对齐。
- 用鼠标右键单击该 TextBox2文本框 控件,然后单击 属性。
- 在属性窗口中将 PasswordChar 属性设置为 *。
- 在工具箱双击 按钮 控件,以将一个 按钮 控件添加到 Form1 窗体。默认状态下,Button1 的 按钮 控件添加到 Form1 窗体中。
- 用鼠标右键单击 Button1Button 控件,然后单击 属性。
- 在属性窗口中设置 文本 属性,以 验证用户。
- 在解决方案资源管理器中,用鼠标右键单击 MyApp,指向 添加,然后再单击 添加新项。 在 添加新项-MyApp 对话框。
- 在 模板 框中,单击 Windows 窗体,然后单击 打开。默认状态下,创建名为 Form2 的 Windows 窗体。
- 将一个 按钮 控件添加到 Form2 窗体。默认状态下,Button1 的 按钮 控件被添加到 Form2 窗体。
- 用鼠标右键单击 Button1Button 控件,然后单击 属性。
- 在属性窗口中设置 文本 属性,以 添加编号。
- 在工具箱双击 标签 控件,以将 Label 控件添加到 Form2 窗体。默认状态下,Label1Label 控件被添加到 Form2 窗体。
- 用鼠标右键单击 Label1标签 控件,然后单击 属性。
- 在属性窗口将 大小 设置为 200,56 的属性。
- 双击 Button1Button 控件,然后将下面的代码添加到 Button1_Click 事件处理程序:
Dim firstnum, secondnum, result As Integer
firstnum = InputBox("Enter the first number")
secondnum = InputBox("Enter the second number")
result = firstnum + secondnum
MessageBox.Show("The sum of the two numbers is:" & result)
编写代码以验证在 Visual Basic.net 应用程序中的 Windows 用户
您可以使用
LogonUser Win32 API 来验证用户名和密码。
LogonUser 函数声明 Advapi32.dll 库中。您可以通过使用
Declare 语句从 Visual Basic.net 应用程序中调用
LogonUser 函数。
您必须将域名称、 用户名称和密码传递给
LogonUser 函数。
LogonUser 函数通过使用这些参数验证用户,并返回一个 boolean 类型的值。如果此函数成功,您收到了句柄类型的值,该值代表 Windows 用户令牌。
WindowsIdentity 对象使用此标记来表示在 Visual Basic.net 或 Visual Basic 2005 应用程序中的 Windows 用户。
WindowsPrincipal 对象使用此
WindowsIdentity 对象验证 Windows 用户的用户权限。
若要写入在 Visual Basic.net 或 Visual Basic 2005 应用程序中实现验证代码,请按照下列步骤操作:
- 在解决方案资源管理器中,右击 Form1.vb,然后单击 查看代码。
- 在 Form1 窗体的顶部添加以下代码:
Imports System.Security.Principal
Imports System.Security.Permissions
Imports System.Runtime.InteropServices
Imports System.Environment
- 找到下面的代码:
- 下面的代码在第 3 步中找到的代码之前添加:
'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
- 找到下面的代码:
- 下面的代码在第 5 步中找到的代码之前添加:
'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 - 在解决方案资源管理器中,右击 Form1.vb,然后单击 视图设计器。
- 双击 Button1Button 控件,然后将下面的代码添加到 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
验证您的 Visual Basic.net 应用程序的工作
若要验证已正确完成验证,请按照下列步骤操作:
- 在 生成 菜单上单击 生成解决方案。
- 在 调试 菜单上单击 开始。
- 在 TextBox1 框中键入用户名称。
- 在 TextBox2 框中键入密码。
- 单击 验证有效性用户。
有关更多的信息,请访问下面的 Microsoft 开发人员网络 (MSDN) 的网站: