HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

List of database users: can you run C# Code in SQL Server Management Studio?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
canyouserversqldatabasemanagementstudiocodelistusers

Problem

I'm trying to obtain a list of users associated with the AD Groups represented by the logons in my database. I understand from my research that the best way to do it is using this code:

{   
Server srv = new Server();   
//Iterate through each database and display.   

foreach ( Database db in srv.Databases) {   
   Console.WriteLine("========");   
   Console.WriteLine("Login Mappings for the database: " + db.Name);   
   Console.WriteLine(" ");   
   //Run the EnumLoginMappings method and return details of database user-login mappings to a DataTable object variable.   
   DataTable d;  
   d = db.EnumLoginMappings();   
   //Display the mapping information.   
   foreach (DataRow r in d.Rows) {   
      foreach (DataColumn c in r.Table.Columns) {   
         Console.WriteLine(c.ColumnName + " = " + r[c]);   
      }   
      Console.WriteLine(" ");   
   }   
}   
}


is this the best approach? Also, this code is in C#, how will I be able to run it in my SSMS?

Solution

The only language1 supported in SQL Server is T-SQL. Having said that, it's pretty simple to use T-SQL to get the details you need.

/*
    Description: Display the Active Directory group 
    membership permission path for all users
*/
SET NOCOUNT ON;
DECLARE @t TABLE (
    [account name] varchar(255)
    , [type] varchar(255)
    , [privilege] varchar(255)
    , [mapped login name] varchar(255)
    , [permission path] varchar(255)
);

INSERT INTO @t
EXEC xp_logininfo;
DELETE 
FROM @t
WHERE type = 'group';
DECLARE @cmd nvarchar(max);
DECLARE cur CURSOR LOCAL FORWARD_ONLY READ_ONLY STATIC FOR
SELECT 'EXEC xp_logininfo @acctname = ''' + name + ''', @option = ''members'';'
FROM master.sys.server_principals
WHERE type = 'G';
OPEN cur;
FETCH NEXT FROM cur INTO @cmd;
WHILE @@FETCH_STATUS = 0
BEGIN
    INSERT INTO @t
    EXEC sp_executesql @cmd;
    FETCH NEXT FROM cur INTO @cmd;
END
SELECT *
FROM @t t
ORDER BY t.[account name];
CLOSE cur;
DEALLOCATE cur;


The results returned show users with access to the SQL Server, and shows the group they are a member of that is providing that access.

You'll need membership in the sysadmin fixed server role or membership in the public fixed database role in the master database with EXECUTE permission granted to execute xp_logininfo. You'll need standard user rights in Active Directory to enumerate the items in the directory.

1 - R is supported in certain configurations, and Python is supported in SQL Server 2017 and above. Generally speaking, you'll want to use T-SQL. Depending on your acceptable security posture, you may be able to use C# or Visual Basic to create a SQLCLR stored procedure that can be called from T-SQL in SQL Server 2005 and above.

Code Snippets

/*
    Description: Display the Active Directory group 
    membership permission path for all users
*/
SET NOCOUNT ON;
DECLARE @t TABLE (
    [account name] varchar(255)
    , [type] varchar(255)
    , [privilege] varchar(255)
    , [mapped login name] varchar(255)
    , [permission path] varchar(255)
);

INSERT INTO @t
EXEC xp_logininfo;
DELETE 
FROM @t
WHERE type = 'group';
DECLARE @cmd nvarchar(max);
DECLARE cur CURSOR LOCAL FORWARD_ONLY READ_ONLY STATIC FOR
SELECT 'EXEC xp_logininfo @acctname = ''' + name + ''', @option = ''members'';'
FROM master.sys.server_principals
WHERE type = 'G';
OPEN cur;
FETCH NEXT FROM cur INTO @cmd;
WHILE @@FETCH_STATUS = 0
BEGIN
    INSERT INTO @t
    EXEC sp_executesql @cmd;
    FETCH NEXT FROM cur INTO @cmd;
END
SELECT *
FROM @t t
ORDER BY t.[account name];
CLOSE cur;
DEALLOCATE cur;

Context

StackExchange Database Administrators Q#212545, answer score: 7

Revisions (0)

No revisions yet.