Get database username from current edit workspace/session

424
5
07-07-2011 06:13 AM
JordanMacDonald
New Contributor
Hi,

I'm looking to get the username from the current geodatabase connection so that I can stamp modifications to features with the current time and username.

Does anyone know what interface I would be able to use in an Add-In to get this information?
0 Kudos
5 Replies
AlexanderGray
Occasional Contributor III
You may have more that one connection in your map for different layers.  Find the layer you want, get its featureclass, from that get its workspace (from Idataset), cast workspace to IDatabaseConnectionInfo2.
0 Kudos
by Anonymous User
Not applicable
Assuming you are in an edit session, you can use IWorkspace.ConnectionProperties to get this information.

E.g. MessageBox.Show(m_editor.EditWorkspace.ConnectionProperties.GetProperty("user").ToString());

Take a look at the example in IPropertySet for the other connection strings.
0 Kudos
JamesCrandall
MVP Frequent Contributor
As an alternative approach in addition to the suggestions already made, have you thought about returning the user account from the Windows Authentication (WindowsIdentity)?

I use this in ALL of my ArcGIS applications because it's great to be able to throw around that user credential in many different components (for db logon, CrystalReporting, etc...).  Anyway, below is a bit of code sampe that you might be able to use.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Security.Principal
Imports System

Public Class Form1

   Private m_User As String


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   Dim logonToken As IntPtr = LogonUser()
   m_User = GetUserName(logonToken)

'Now you can use the m_User in your app for reporting/doing stuff with the current user that is
'logged into with Windows Authentication

End Sub

Private Function LogonUser() As IntPtr
        Dim accountToken As IntPtr = WindowsIdentity.GetCurrent().Token
        Return accountToken
End Function

Function GetUserName(ByVal logonToken As IntPtr) As String
        Dim windowsIdentity As New WindowsIdentity(logonToken)
        Dim parts() As String = Split(windowsIdentity.Name, "\")
        Dim username As String = parts(1)

        Return username

End Function
0 Kudos
NeilClemmons
Regular Contributor III
As an alternative approach in addition to the suggestions already made, have you thought about returning the user account from the Windows Authentication (WindowsIdentity)?

I use this in ALL of my ArcGIS applications because it's great to be able to throw around that user credential in many different components (for db logon, CrystalReporting, etc...).  Anyway, below is a bit of code sampe that you might be able to use.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Security.Principal
Imports System

Public Class Form1

   Private m_User As String


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   Dim logonToken As IntPtr = LogonUser()
   m_User = GetUserName(logonToken)

'Now you can use the m_User in your app for reporting/doing stuff with the current user that is
'logged into with Windows Authentication

End Sub

Private Function LogonUser() As IntPtr
        Dim accountToken As IntPtr = WindowsIdentity.GetCurrent().Token
        Return accountToken
End Function

Function GetUserName(ByVal logonToken As IntPtr) As String
        Dim windowsIdentity As New WindowsIdentity(logonToken)
        Dim parts() As String = Split(windowsIdentity.Name, "\")
        Dim username As String = parts(1)

        Return username

End Function


An easier way to do this in .NET is System.Environment.UserName.
0 Kudos
AlexanderGray
Occasional Contributor III
Windows user name is fine if you are using OS authentication in your connection.  If not, this won't tell you what user is connected to the database.
0 Kudos