2008-08-12 13 views
9

मैं अपनी कंपनी के लिए सॉफ़्टवेयर के एक टुकड़े के लिए एक spec पर काम कर रहा हूं और ऑडिटिंग सिस्टम के हिस्से के रूप में मुझे लगता है कि वर्तमान सक्रिय निर्देशिका उपयोगकर्ता को पकड़ने का कोई तरीका होने पर यह साफ होगा।क्या मौजूदा सक्रिय निर्देशिका उपयोगकर्ता को पकड़ने के लिए एमएस एक्सेस के लिए कोई तरीका है?

तरह

उम्मीद है कि कुछ:

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

उत्तर

6

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 
+0

मैं आपके लिंक-केवल उत्तर को कम करने का लुत्फ उठा रहा था, लेकिन मेह, चलो बस सौभाग्य से सड़े हुए लिंक को उद्धृत न करें;) –

+0

सभी अच्छे। प्रतिबिंब पर एक सुंदर shitty जवाब था। शुरुआती दिनों में, केवल जवाबों को लिंक न करें, मेरी रक्षा में आज जैसी चीजें उतनी कठोर नहीं थीं;) वैसे भी, संपादन के लिए धन्यवाद। – JamesSugrue

2

पर निर्भर करता है वैध रहने के लिए पर्यावरण चर एक बुरा विचार है, क्योंकि उन्हें उपयोगकर्ता सत्र में आसानी से बदला जा सकता है।

+0

बहुत अच्छा मुद्दा! – Yarik

2

डेविड ने पर्यावरण चर का उपयोग करने के जोखिम के बारे में बहुत अच्छा मुद्दा बनाया। मैं केवल इतना जोड़ सकता हूं कि पर्यावरण चर के साथ अन्य समस्याएं हो सकती हैं। बस हमारे 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 
+0

यदि आप वास्तव में जेट उपयोगकर्ता-स्तरीय सुरक्षा का उपयोग कर रहे हैं तो CurrentUser() के साथ कुछ भी गलत नहीं है। दूसरी तरफ, यदि आपको जेट यूएलएस की कोई आवश्यकता नहीं है, तो विंडोज लॉगऑन एक उत्कृष्ट विकल्प है (हालांकि आपको अभी भी अपने ऐप में समूह सदस्यता के किसी प्रकार की तालिका को बनाए रखना पड़ सकता है)। –

3

यहाँ मेरी संस्करण है:

'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: यह आप की तरह कुछ भी लायेगा।

संबंधित मुद्दे

 संबंधित मुद्दे