I have a two databases Source and Target hosted in the same SQL Server 2008 R2 instance. I have a Login which is mapped to Users in both databases. Each mapped User has a Role in its respective database which grants access to all objects in that database.
I have a cross-database trigger which propagates updates from a table in Source to a table in Target.
When I connect to the server in SQL Server Managment Studio with the Login and run an update query on Source, the cross-database update succeeds.
But when an equivalent update is initiated using the same Login from within an application, the trigger fails with error:
The server principal 'name' is not able to access the database 'Target' under the current security context.
By running a trace with SQL Server Profiler, I can verify that the update is being run under the same LoginName in both cases.
Questions:
What is special about SSMS that allows the cross-database trigger to work?
What does "current security context" mean?
What do I need to modify to allow the update+trigger to work in the Application's context?
I've read about object ownership chaining, and I've not fully investigated that yet. But since it works in SSMS, I'm tempted to believe that a broken ownership chain is not the issue. But I could be wrong about that! I'd be really grateful for any diagnosis suggestions.
Update: I'm attempting to implement srutzky's suggestion. This is what I have run so far.
use [SourceDB];
create certificate [Access_TargetDB]
encryption by password = 'password123'
with subject = 'Cross-DB Access to TargetDB',
expiry_date = '2099-12-31';
add signature to [MyTrigger]
by certificate [Access_TargetDB]
with password = 'password123';
backup certificate [Access_TargetDB]
to file = 'C:\Access_TargetDB.cert';
use [TargetDB];
create certificate [SourceDB]
from file = 'C:\Access_TargetDB.cert';
create user [SourceDBUser] for certificate [SourceDB];
exec sp_addrolemember 'StandardUserRole', 'SourceDBUser';
I am still getting the same error when attempting to update the table in SourceDB with the Application Role context.
I'm attempting to implement srutzky's 2nd suggestion. In addition to the above, I have also run the following. But the suggestion assumes a target stored procedure which I do not have, so maybe this isn't actually a valid test.
use master;
create certificate [SourceDB]
from file = 'C:\SourceDB.cert';
create login [SourceDBLogin] from certificate [SourceDB];
grant authenticate server to [SourceDBLogin];
Still no change in the permissions available in the Application Role context.
publicrole when selecting fromsys.login_token;? Step 2 in your answer doesn't show that field. I also just noticed that you have updated the question with more details. I noticed that you didn't backup/restore the private key..not sure yet if that has any impact here, but I have never tried to omit that part. Nor have I tried adding the User to a Role instead of granting direct permissions. Also, look at this answer http://dba.stackexchange.com/a/166280/30859 as it required creating a login and granting itAUTHENTICATE SERVER. – Solomon Rutzky Mar 26 '17 at 14:52usagecolumn to Step 2 in my answer. Its result is different in SSMS vs. Application Role context. – dlh Mar 26 '17 at 15:13PUBLICwould beDENY ONLY. That'is a problem, one that is preventing my initial response, in that answer linked in my previous comment, from working there as well. It's still worth trying the Login, granted withAUTHENTICATE SERVERfirst just to be sure. But if that doesn't work, then it might require enabling theguestUser and then creating a stored procedure in the Target DB thatguestcan execute and it does the INSERT or UPDATE, and it only runs if the Login isnameand is executed by the Trigger. I will try to prove it out in that linked answer. – Solomon Rutzky Mar 26 '17 at 15:21TargetDBhas no master key (and I am not yet sure what the side effects of creating one will be). – dlh Mar 26 '17 at 15:40ENCRYPTION BY PASSWORD. :-) DMK is for when you aren't specifying a password. So you can still backup and restore the private key here. Just use the same password. – Solomon Rutzky Mar 26 '17 at 15:44TargetDBrather than relying on the Database Role. No change in the outcome. – dlh Mar 26 '17 at 21:34authenticate serverdo I have to refactor my trigger to use a stored procedure inTargetDB? (Nontrivial because I'm joining tables in both databases to theinsertedanddeletedtables). – dlh Mar 27 '17 at 01:37