【问题标题】:Is there a way for MS Access to grab the current Active Directory user?MS Access 有没有办法获取当前的 Active Directory 用户?
【发布时间】:2008-08-12 17:03:28
【问题描述】:

我正在为我的公司制定一个软件规范,作为审计系统的一部分,我认为如果有办法抓住当前的 Active Directory 用户,那就太好了。

希望是这样的:

Dim strUser as String
strUser = ActiveDirectory.User()
MsgBox "Welcome back, " & strUser

【问题讨论】:

    标签: ms-access active-directory


    【解决方案1】:

    Try this article - 我有一些代码在工作中,如果没有,我会工作......

    相关引述:

    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
                        (ByVal IpBuffer As String, nSize As Long) As Long
    Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
                        (ByVal lpBuffer As String, nSize As Long) As Long
    
    Function ThisUserName() As String
        Dim LngBufLen As Long
        Dim strUser As String
    
        strUser = String$(15, " ")
        LngBufLen = 15
    
        If GetUserName(strUser, LngBufLen) = 1 Then
            ThisUserName = Left(strUser, LngBufLen - 1)
        Else
            ThisUserName = "Unknown"
        End If
    End Function
    
    Function ThisComputerID() As String
        Dim LngBufLen As Long
        Dim strUser As String
    
        strUser = String$(15, " ")
        LngBufLen = 15
    
        If GetComputerName(strUser, LngBufLen) = 1 Then
            ThisComputerID = Left(strUser, LngBufLen)
        Else
            ThisComputerID = 0
        End If
    End Function
    

    【讨论】:

    • 我很想对您的仅链接答案投反对票,但是,嗯,让我们引用幸运的不是烂链接;)
    • 一切顺利。经过反思,这是一个非常糟糕的答案。在我的辩护中,早期的时候,不只是链接答案的事情不像今天那么严格;)无论如何,感谢您的编辑。
    【解决方案2】:

    这是我的版本:它会获取您喜欢的任何内容:

    'gets firstname, lastname, fullname or username
    Public Function GetUser(Optional whatpart = "username")
        Dim returnthis As String
        If whatpart = "username" Then GetUser = Environ("USERNAME"): Exit Function
        Set objSysInfo = CreateObject("ADSystemInfo")
        Set objUser = GetObject("LDAP://" & objSysInfo.USERNAME)
        Select Case whatpart
            Case "fullname": returnthis = objUser.FullName
            Case "firstname", "givenname": returnthis = objUser.givenName
            Case "lastname": returnthis = objUser.LastName
            Case Else: returnthis = Environ("USERNAME")
        End Select
        GetUser = returnthis
    End Function
    

    I got the original idea from Spiceworks.

    【讨论】:

      【解决方案3】:

      依赖环境变量保持有效是个坏主意,因为它们很容易在用户会话中更改。

      【讨论】:

        【解决方案4】:

        David 对使用环境变量的风险提出了很好的观点。我只能补充一点,环境变量可能还有其他问题。看看我们 5 年前的项目中的这个实际代码片段:

        Public Function CurrentWorkbenchUser() As String
        
            ' 2004-01-05, YM: Using Application.CurrentUser for identification of 
            ' current user is very problematic (more specifically, extremely 
            ' cumbersome to set up and administer for all users). 
            ' Therefore, as a quick fix, let's use the OS-level user's 
            ' identity instead (NB: the environment variables used below must work fine
            ' on Windows NT/2000/2003 but may not work on Windows 98/ME)
            ' CurrentWorkbenchUser = Application.CurrentUser
            '
            ' 2005-06-13, YM: Environment variables do not work in Windows 2003. 
            ' Use Windows Scripting Host (WSH) Networking object instead.
            ' CurrentWorkbenchUser = Environ("UserDomain") & "\" & Environ("UserName")
            '
            ' 2007-01-23, YM: Somewhere between 2007-01-09 and 2007-01-20, 
            ' the WshNetwork object stopped working on CONTROLLER3. 
            ' We could not find any easy way to fix that.
            ' At the same time, it turns out that environment variables 
            ' do work on Windows 2003.
            ' (Apparently, it was some weird configuration problem back in 2005: 
            ' we had only one Windows 2003 computer at that time and it was 
            ' Will's workstation).
            '
            ' In any case, at the time of this writing, 
            ' returning to environment variables 
            ' appears to be the simplest solution to the problem on CONTROLLER3.
            ' Dim wshn As New WshNetwork
            ' CurrentWorkbenchUser = wshn.UserDomain & "\" & wshn.UserName
        
            CurrentWorkbenchUser = Environ("USERDOMAIN") & "\" & Environ("USERNAME")
        
        End Function
        

        【讨论】:

        • 如果您实际使用的是 Jet 用户级安全性,那么 CurrentUser() 没有任何问题。另一方面,如果您不需要 Jet ULS,那么 Windows 登录是一个很好的选择(尽管您可能仍然需要在您的应用程序中维护某种组成员资格表)。
        猜你喜欢
        • 2020-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多