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

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

问题描述

我有一个Access应用程序,要求用户输入他们的Windows域用户名和密码进入。我用下面的VBA code来实现:

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

我注意到,有时当输入正确的信息,访问被拒绝。我试着调试一次,它给了错误:网络路径没有被发现。
关于设置oADsObject = oADsNamespace.OpenDSObject)行。

不知道为什么这有时会发生。它是更好的转换,而不是为LDAP?我曾经试过,但不能正确构造LDAP URL。

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.

推荐答案

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

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天全站免登陆