Is an application role active on a SQL server connection?

1.5k Views Asked by At

How can i check, if an (and which) application role is active for a connection (DbConnection object) to the sql server?

My problem is, that all database objects have access granted for my application role, but when executing a select statement i get an "access denied" error. I use a single connection object with application role activated, but I fear, that either EntityFramework or the DI container creates another DbConnection object due to a configuration fault.

1

There are 1 best solutions below

2
On BEST ANSWER

Doh. On re-reading the documentation for sp_setapprole, I see this sample code:

DECLARE @cookie varbinary(8000);
EXEC sp_setapprole 'Sales11', 'fdsd896#gfdbfdkjgh700mM'
    , @fCreateCookie = true, @cookie = @cookie OUTPUT;
-- The application role is now active.
SELECT USER_NAME();
-- This will return the name of the application role, Sales11.

So it would appear that accessing USER_NAME() will do the trick.