VBA:使用 Windows 身份验证登录 [英] VBA: Login using Windows Authentication

查看:31
本文介绍了VBA:使用 Windows 身份验证登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Access 应用程序,要求用户输入他们的 Windows 域用户和密码才能进入.我已使用以下 VBA 代码来完成此操作:

Function WindowsLogin(ByVal strUserName As String, ByVal strpassword As String, ByVal strDomain As String) As Boolean'验证使用 Active Directory 输入的用户和密码.On Error GoTo IncorrectPasswordDim oADsObject, oADsNamespace As Object将 strADsPath 调暗为字符串strADsPath = "WinNT://" &字符串域设置 oADsObject = GetObject(strADsPath)设置 oADsNamespace = GetObject("WinNT:")设置 oADsObject = oADsNamespace.OpenDSObject(strADsPath, strDomain & "" & strUserName, strpassword, 0)WindowsLogin = True '访问权限已授予退出子:退出函数密码错误:WindowsLogin = False '访问被拒绝恢复ExitSub结束功能

我注意到有时当信息输入正确时,访问被拒绝.我尝试调试一次,它给出了错误:找不到网络路径." 在 Set oADsObject = oADsNamespace.OpenDSObject) 行上.

不知道为什么有时会发生这种情况.改为转换为 LDAP 会更好吗?我已尝试但无法正确构建 LDAP URL.

解决方案

如果用户已经通过 Windows 登录进行身份验证,为什么还要让他们再次输入详细信息?

如果你想知道哪个用户登录了,你可以很容易的通过下面的函数获取用户名:

<上一页>声明函数 IGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal sBuffer As String, lSize As Long) As Long函数 GetUserName() 作为字符串出错时继续下一步将 sBuffer 作为字符串变暗将 lSize 变暗暗淡 x 长sBuffer = 空间$(32)lSize = Len(sBuffer)x = IGetUserName(sBuffer, lSize)GetUserName = left$(sBuffer, lSize - 1)结束功能

I have a an Access App that requires the user to enter their Windows domain user and password to enter. I have used the following VBA code to accomplish this:

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

I notice that sometimes when the information is entered correctly, access is denied. I tried to debug once and it gave the error: "The network path was not found. " on the Set oADsObject = oADsNamespace.OpenDSObject) line.

Not sure why this occurs sometimes. Is it better to convert to LDAP instead? I have tried but can't construct the LDAP URL correctly.

解决方案

If the user is already authenticated via their Windows login, why make them enter the details again?

If you need to know which user is logged in, you can get the username very easily by the following function:

Declare Function IGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal sBuffer As String, lSize As Long) As Long

Function GetUserName() As String

    On Error Resume Next

    Dim sBuffer As String
    Dim lSize As Long
    Dim x As Long

    sBuffer = Space$(32)
    lSize = Len(sBuffer)
    x = IGetUserName(sBuffer, lSize)
    GetUserName = left$(sBuffer, lSize - 1)

End Function

这篇关于VBA:使用 Windows 身份验证登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆