0

因此,我一直在尝试为工具添加一定程度的安全性,结果发现了这篇文章。该代码在网络/域上时有效,但我需要以某种方式将其用于远程且不在网络上的人,也不是 vpn'ed 到网络中的人。这可能吗?我在这里边学习边学习,所以一开始这甚至可能都不可行。只是寻找所有可能的途径。

示例代码:

Function WindowsLogin(ByVal strUserName As String, ByVal strpassword As String, ByVal strDomain As String) As Boolean
    'Authenticates user and password entered with Active Directory. 

    On Error GoTo IncorrectPassword

    Dim oADsObject, oADsNamespace As Object
    Dim strADsPath As String

    strADsPath = "WinNT://" & strDomain
    Set oADsObject = GetObject(strADsPath)
    Set oADsNamespace = GetObject("WinNT:")
    Set oADsObject = oADsNamespace.OpenDSObject(strADsPath, strDomain & "\" & strUserName, strpassword, 0)

    WindowsLogin = True    'ACCESS GRANTED

ExitSub:
    Exit Function

IncorrectPassword:
    WindowsLogin = False   'ACCESS DENIED
    Resume ExitSub
End Function

编辑:所以@user2140261 告诉我尝试LogonUser来自 Advapi32.dll 的函数,如下所示:

Private Declare Function LogonUser Lib "advapi32.dll" Alias "LogonUserA" (ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, ByVal dwLogonType As UInteger, ByVal dwLogonProvider As UInteger, ByRef phToken As IntPtr) As Boolean

Sub LoginTest()

    Dim logname As String
    Dim logpass As String
    Dim domainstring As String

    logname = "username"
    logpass = "password"
    domainstring = "domain.com"

    Call WindowsLogin(logname, logpass, domainstring)

End Sub

出于某种原因,这会使 Excel 一起崩溃。有什么理由吗?

4

2 回答 2

1

不确定WindowsLogin您的原始代码中的子是什么,但请尝试以下操作:

Private Declare Function LogonUser Lib "Advapi32" Alias "LogonUserA" _
(ByVal lpszUsername As String, ByVal lpszDomain As String, ByVal lpszPassword As String, _
ByVal dwLogonType As Long, ByVal dwLogonProvider As Long, phToken As Long) As Long

Public Sub LogUserOn()
 Dim strUserName As String
 Dim strPassword As String
 Dim strDomain As String
 Dim bResult As Boolean

 strUserName = "UserName"
 strPassword = "Password"
 strDomain = "Domain"

 bResult = LogonUser(strUserName, strDomain, strPassword, 2, 0, 0)

 If bResult Then
    MsgBox "Successfully Logged User In"
 Else: MsgBox "And Error Occured While Trying To Log User In " & vbCrLf _
            & "Error Code:" & Err.LastDllError
 End If

End Sub
于 2013-10-25T19:50:23.167 回答
0

正如您引用的帖子中的答案所说,如果您只需要知道谁在使用该应用程序,并基于此应用安全性,那么您可以从操作系统获取登录用户的名称。用户可以在断开连接时使用他们的缓存凭据登录到他们的笔记本电脑,并且您的应用程序将能够根据操作系统声明的身份锁定所需的任何内容。

更新

有几种方法可以从操作系统获取用户的身份。一是环境变量%USERDOMAIN%%USERNAME%. 另一个是WScript.Network对象:

Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Domain = " & WshNetwork.UserDomain
WScript.Echo "User Name = " & WshNetwork.UserName

您可以验证用户是否登录到正确的域,然后提供对受保护功能的访问。

于 2013-10-25T17:54:35.997 回答