MS Access가 현재 Active Directory 사용자를 확보할 수 있는 방법이 있습니까?
-
08-06-2019 - |
문제
저는 회사의 소프트웨어 사양을 작성하고 있으며 감사 시스템의 일부로 현재 Active Directory 사용자를 확보할 수 있는 방법이 있다면 좋을 것이라고 생각합니다.
바라건대 다음과 같습니다.
Dim strUser as String
strUser = ActiveDirectory.User()
MsgBox "Welcome back, " & strUser
해결책
이 기사를 사용해보십시오 - 직장에 작동하는 코드가 있는데, 그렇지 않으면 작동합니다...
관련 인용문:
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
다른 팁
내 버전은 다음과 같습니다.그것은 당신이 좋아하는 것을 가져올 것입니다:
'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
유효한 상태를 유지하기 위해 환경 변수에 의존하는 것은 좋지 않은 생각입니다. 왜냐하면 환경 변수는 사용자 세션 내에서 쉽게 변경될 수 있기 때문입니다.
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
제휴하지 않습니다 StackOverflow