How to get the WINDOWS USER within "EXECUTE AS OWNER" - sql

Is there a function which returns the WINDOWS USER (NOT the database user!) within a procedure which is "EXECUTE AS OWNER" ?
Will ORIGINAL_LOGIN() do this, or does that only return the database user ?
Consider the following situation:
Windows Users "joe" and "anne" are part of a Active Directory Group, e.g. "PLANNERS"
The SQL Server Database has "PLANNERS" as Server Login and as Database User.
There is stored procedure WITH EXECUTE AS OWNER
Now, anne is logging into the SQL Database via Windows Authentification (so she is database user "PLANNERS"), and she calls the procedure. In this procedure, I want to retrieve the Windows User ("anne").

ORIGINAL_LOGIN() is the correct answer.
Just needed it again ;)
Thanks #deterministicfail

Related

How to create a contained database and user in azure sql

I am trying to create a contained user for just one database in Azure SQL Server,
I have tried using the sp_configure keyword, it says it is not available in the version of the SQL Server I am using.
Also, I used the Alter database statement, I got the error below:
ALTER DATABASE statement failed; this functionality is not available
in the current edition of SQL Server.
Please, how can I solve this problem???
You do not need to run the ALTER DATABASE ... SET CONTAINMENT command on Azure SQL DBs to accept contained users - it is already enabled by default. You simply need to create the user with just a login and password. A simple example of a contained user with password:
CREATE USER yourUser WITH PASSWORD = 'yourPassword';
See the official documentation for more examples:
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-user-transact-sql?view=sql-server-ver15#e-creating-a-contained-database-user-with-password
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-user-transact-sql?view=sql-server-ver15#f-creating-a-contained-database-user-for-a-domain-login
sp_configure is not supported in Azure SQL database, even use the Alter database:
In Azure SQL database, login is used to login the Azure SQL server, user is to connect to the database. User is database level, and login is server level.
Create login in master DB(( Login must be created in master DB)):
CREATE LOGIN AbolrousHazem
WITH PASSWORD = '340$Uuxwp7Mcxo7Khy';
Then we can create user in user DB( create the database contained user in user DB):
CREATE USER AbolrousHazem FOR LOGIN AbolrousHazem;
GO
For more details, please ref: https://learn.microsoft.com/en-us/azure/azure-sql/database/logins-create-manage

Getting the name of the user who executed the Stored Procedure

I know this can be done in code (C#) by using WindowsIdentity, but is there a way to do this within the SP itself?
I tried using the USER function but it's returning dbo instead of the name. Is this because the user is the owner of the database? If that's so, how can I get the db owner's login name?
EDIT:
The application that executes the SP uses the sa user account, so if I use SYSTEM_USER, the returned value is sa.
SELECT SYSTEM_USER
Will return name of user which excecute code in SQL. More in this aricle
If SQL server service work from sa there is no way to get (windows) username from SQL Server side. I suppose it is security issue. Most information about connection to your SQL Server you may get from table sys.dm_exec_connections. There are stored IP addreses and port s and other useful things for existings connections.

Run stored procedure that use catalog procedures of SSIS with windows user with minimum rights

I need run .dtsx packages mounted in SSIS catalog with parameters, throught an stored procedure, because one aplication fired dtsx using stored procedure with parameter values
I have created the stored procedure using
SSISDB.Catalog.Create_Execution
Set_Execution_Parameter_Value and
Start_Execution.
This works, but for run the user need have db_owner privilegies on principal_db and ssis_admin on SSISD this will only run if the executing user is a windows account, no problem with this but I need the windows account have minimal rigths on principal_db, I know I can't do it using Sql user because is necesary an windows account but when I create my procedure give execution permition for execute stored proc to an win user in the principal database, create the win user on SSISDB and give permision on catalog procedures
SSISDB.Catalog.Create_Execution
Set_Execution_Parameter_Value
Start_Execution
and add to my stored procedure the clause "execute as owner",show me next error
I tried using Execute As and specifying a windows account but I get the error
The current security context cannot be reverted. Please switch to the
original database where 'Execute As' was called and try it again.
I tried using "execute as self" and when I try run the procedure with another win user (that give permision) show me is necesary windows account.
My user has permision on
db_owner
sysadmin and
ssis_admin
I think I need come into my permissions to an user for execute this, considering that my procedure working with 2 databases principal_db (where record my procedure) and SSISDB (where live the catalog procedures for create execution and put parameters).
Somebody can help me?
SSIDB admin permission required to run DTSX package.
Package references DB2 proc, needing DBO so proc MUST have EXECUTE as OWNER
AND Proc has GRANT Execute to Win User
Schedule job step owned by WIN USER... this so? and fails?

SQL Server: how to query when and where (which database) logins take a connection?

I have many databases in one SQL Server instance. Also I have many individual domain users for logins and also domain groups as logins. These logins are attached to databases as database users. Now I would like to create a report (or a query) to find out when and where (which database) these logins take a connection. I tried to find this information from sys-schema, but I could not find this kind of information. Where SQL Server store this information and how to create this kind of report? I would also like to automate this using a stored procedure.
in the login stored procedure, along with the username, add a GetDate() and DB_NAME() to each record in a table called LoginData (or whatever you prefer)
You could set up a SQL trace logging all login events then just query that.

odbc32 and SQLBrowseConnect Help

I'm making a call to odbc32.dll (SQLBrowseConnect) to return a list of databases on a sql server.
From running a trace I can see the query being executed is
select name from master..sysdatabases where has_dbaccess(name)=1
If the credentials I pass aren't the sa user it returns just the system databases. Is there anyway I can use SQLBrowseConnect with another user (whose default database is also not guarenteed to be the master database) to return all databases on the server?
Also I want to avoid smo objects
The query does work without sysadmin credentials.
You need to ensure that you / the credentials you are using are at least in the public database role on each of the databases that you need to connect to.
As you would guess, select name from master..sysdatabases returns all database names irrespective of your access to the DB.