Skip to content

Instantly share code, notes, and snippets.

@ronmichael
Created October 25, 2013 14:14
Show Gist options
  • Save ronmichael/7155306 to your computer and use it in GitHub Desktop.
Save ronmichael/7155306 to your computer and use it in GitHub Desktop.
Apply custom logic to prevent some users from logging into MSSQL by applying a logon trigger. This particular example looks at the user's name as well as IP address.
CREATE TRIGGER [access_trigger]
ON ALL SERVER
with execute as 'sa' -- needed to query sys.dm_exec_connections table
FOR LOGON
AS
BEGIN
if original_login() not in ('superadmin', 'anothersuperadmin', 'mydomain\admin')
and exists (
select * from sys.dm_exec_connections
where session_id = @@SPID
and client_net_address != '<local machine>' -- allow anyone logging on from local server
and client_net_address not like '102.%' -- allow anyone logging on from 102.x.x.x network
) rollback;
END
GO
ENABLE TRIGGER [access_trigger] ON ALL SERVER
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment