因此,我一直在尝试为工具添加一定程度的安全性,结果发现了这篇文章。该代码在网络/域上时有效,但我需要以某种方式将其用于远程且不在网络上的人,也不是 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 一起崩溃。有什么理由吗?