|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, July 30, 2012 10:34 AM
Points: 173,
Visits: 105
|
|
We are using the technique with connection pooling (although a WinForms app) for user/application context information required for logging purposes (used in triggers). By always setting the CONTEXT_INFO as part of our connection logic, we do not have to worry about "leftovers" from previous users. But in any case, I believe the connection reset that takes place when reusing a connection in the pool clears the previous user's CONTEXT_INFO (but you should verify this yourself).
Regards,
Tore Bostrup
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Saturday, May 18, 2013 1:35 PM
Points: 301,
Visits: 473
|
|
>>As long as the trigger is enabled you can make sure that the logic will work. Maybe could also create a database-level trigger using the same technique, to make sure that the table wasn't altered or dropped outside of an approved way, to prevent this? Great article, thank you... >L<
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Saturday, May 18, 2013 1:35 PM
Points: 301,
Visits: 473
|
|
I thought maybe we could do this with EVENTINFO() in a trigger, but the calling proc isn't included in the schema (would be nice!). Looking at the docs, it seems maybe you could use context info more pervasively in a system and then do something like this: SELECT context_info AS MyCtxInfo FROM sys.dm_exec_requestsWHERE session_id = @@SPIDAND request_id = CURRENT_REQUEST_ID();... ? but I am not sure, I don't understand how CURRENT_REQUEST_ID() works or whether you will ever see more than one result from the above code. Anyone?? >L<
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, January 26, 2012 9:27 AM
Points: 1,
Visits: 17
|
|
Very Good Article, I was not aware of it...
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, December 27, 2006 2:51 PM
Points: 2,
Visits: 1
|
|
Very useful article ! I don't know about this until now. Thank you. David N Nguyen.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 8:09 AM
Points: 1,558,
Visits: 1,396
|
|
This procedure wouldn't work for me in SQL 2005. Casting the Context_Info as varchar did not work. I had to cast the string as binary(128). I had to rewrite the trigger as following: IF (Select CONTEXT_INFO()) <> Cast('uspModifyEmployees' as binary(128))Also, in SQL 2005, Context_Info can be Null which it is for an ad hoc query. A Null Context_Info would not be caught by the trigger since Null is neither equal nor not equal to anything. So, I further had to modify it to account for null: IF (Select IsNull(CONTEXT_INFO(), Cast('' as binary(128)))) <> Cast('uspModifyEmployees' as binary(128))Of course, this doesn't really stop me from doing ad hoc queries, as I can still do this: Set Context_Info 0x7573704D6F64696679456D706C6F79656573 Delete From EmployeesWhere EmpID = 1SET CONTEXT_INFO 0x0
My blog: SQL Soldier Twitter: @SQLSoldier Microsoft Certified Master: SQL Server 2008 Sr. Product Consultant and Chief SQL Server Evangelist @ Idera My book: Pro SQL Server 2008 Mirroring
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, February 14, 2013 2:07 AM
Points: 126,
Visits: 123
|
|
Interesting, but please all do not forget that this is what PERMISSIONS are for in SQL Server. I find it's best to use a thing according to its intended purpose first, and only tweak/hack it when absolutely necessary. This trick is not as secure, it's harder to understand, it would potentially make life miserable for someone working on the system who is unfamiliar with it, etc. etc.
If you want a table to be read only, then:
1. Set permissions for the user(s) or role(s) as Select for the table
2. Set permissions to grant execute on the stored proc.
This works perfectly.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 8:09 AM
Points: 1,558,
Visits: 1,396
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, March 18, 2013 1:29 AM
Points: 1,
Visits: 56
|
|
| I agree totally, in fact I go so far to say that this article is going to confuse a lot of people new to SQL Server. What the author is proposing really should be done through permissions. Simply no permissions should be granted to the table directly and execute access granted to the stored procedure itself. I'm not sure if the article was intended as an academic exercise but it seems fairly impractical approach to me.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 7:23 AM
Points: 199,
Visits: 136
|
|
Informative article... i was not aware of the CONTEXT_INFO. I feel that use of CONTEXT_INFO in the scripts will be creating dependencies with the developers i.e., it will be mandatory to use this in each script. Setting up the permissions will be the correct and secure option.
|
|
|
|