首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >验证Windows用户名/pwd是否具有管理权限

验证Windows用户名/pwd是否具有管理权限
EN

Stack Overflow用户
提问于 2014-06-19 22:05:20
回答 1查看 100关注 0票数 2

我正在编写一个从非特权用户帐户运行的Windows窗体应用程序。

对于一个操作,我需要提示一个用户名/pwd一个帐户与管理特权。

因此,该应用程序实际上不必从特权帐户运行;但是用户必须指定一个管理帐户才能允许执行某些操作。

有人知道如何将用户名/pwd验证为具有管理权限的帐户吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-06 17:14:23

正如Harry评论的那样,您可以使用以下方法验证用户名/密码:

代码语言:javascript
复制
Private Declare Auto Function CloseHandle Lib "kernel32.dll"   
                             (ByVal  clsTokenToClose As IntPtr) As Integer

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

Const DOMAIN_NAME As String = "MYDOMAIN"
Dim token As IntPtr

'Use the Win32API LogonUser to authenticate UserName and Password.
'If successful, a token representing the user is returned.

 If LogonUser("UserName", DOMAIN_NAME, "password", LOGON32_LOGON_BATCH,  
              LOGON32_PROVIDER_DEFAULT, token) Then

     'The token is used to create a WindowsIdentity, which is in turn
     'used to create a WindowsPrincipal.  The WindowsPrincipal is checked
     'to see if it belongs to the desired group in ActiveDirectory.

     Dim WIdent As New WindowsIdentity(token)

     Dim WPrincipal As New WindowsPrincipal(WIdent)

     If WPrincipal.IsInRole("Administrators") Then 
        'User has admin privilege, carry on.

     End If

     CloseHandle(token)

 End If

确保将WPrincipal.IsInRole调用中的"Administrators“替换为要对其进行检查的组。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24316977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档