﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / Security (SS2K8)  / Limit Concurrent Logins by database and/or user ID / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 19:46:20 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>Sorry for the delay in responding. I just returned from vacation.[quote][b]kwoznica (11/29/2012)[/b][hr]OPC, When I tested it at the SQL level I was logging into the sql server as the user in the limitedlogins table. At the application level I would receive the attached error message. I then added the principal logon_trigger_login to the sysadmin role and the application error went away. Also I was able to login as a user not in the limited login table, which is the ideal scenario.[/quote]Excellent. A working model that proves the concept.[quote]Do you see any issue with having the logon_trigger_login principal a member of the sysadmin role? Would a lesser role accomplish the same thing?[/quote]I do not see any issues with it as it relates to the trigger itself but do not recommend having anyone in the sysadmin role that does not technically need it, and this login should not need to be in the sysadmin role so I would recommend to continue until you can once again remove the login from the role. Two other items to check:1. Did you add a user for the login in the database where the user-table resides, and then grant that database user select permissions on the user-table?From and earlier post:[code="sql"]USE GK50LIVE;GOCREATE USER [logon_trigger_login] FROM LOGIN [logon_trigger_login] WITH DEFAULT_SCHEMA = [dbo];GOGRANT SELECT ON dbo.LimitedLogins TO logon_trigger_login;GO[/code]The login needs access to the user table which implies it must have a user in the database and have permission to select from it.2. Did you grant the login permissions to view the server-state? From an earlier post:[code="sql"]USE master;GOGRANT VIEW SERVER STATE TO logon_trigger_login;GO[/code]Because we are referring to sys.dm_exec_sessions in the trigger code the executing the trigger must be granted the VIEW SERVER STATE permission.</description><pubDate>Mon, 03 Dec 2012 15:15:54 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>OPC, When I tested it at the SQL level I was logging into the sql server as the user in the limitedlogins table. At the application level I would receive the attached error message. I then added the principal logon_trigger_login to the sysadmin role and the application error went away. Also I was able to login as a user not in the limited login table, which is the ideal scenario. I still want to test a few scenarios but with your suggestions this looks to be working. Do you see any issue with having the logon_trigger_login principal a member of the sysadmin role? Would a lesser role accomplish the same thing?Thanks for your help.</description><pubDate>Thu, 29 Nov 2012 13:40:19 GMT</pubDate><dc:creator>kwoznica</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>It might not be a bad idea to bypass the logic for sysadmins using IS_SRVROLEMEMBER().E.g.IF NOT IS_SRVROLEMEMBER ( 'sysadmin', ORIGINAL_LOGIN())AND ... The check for num allowed in the tableThen rollback.When you say you tested 'at the sql level' were you by chance using EXECUTE AS?</description><pubDate>Tue, 27 Nov 2012 04:11:36 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>OPC, It does work with a non-sysadmin user if they are in the table. I also only tested this at the sql level so I still need to see what happens when I . I actually locked myself out of the database after putting the trigger in place because I did not include my login in the LoginTable and I am part of sysadmin. Fortunately I still had a live connection and the trigger didn't disconnect me so I was able to drop it. Otherwise if a login is not in the table they are not able to establish a session. I kept the original code from this post for the trigger. The only changes I made were to the table as per your performance suggestions. Let me know what you think. In the meantime I'll test a bit further. I appreciate the assistance.</description><pubDate>Mon, 26 Nov 2012 09:52:59 GMT</pubDate><dc:creator>kwoznica</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>[quote][b]kwoznica (11/23/2012)[/b][hr]OPC, How could I expand on this so that if a login is not in the login table then the login is allowed. Right now if a login is not in that particular table then the login is denied.[/quote]I am on my tablet so I cannot test, but from just reading the code it seems like it should work even if there is no row in the login-table. If there is no row then the selection of NUM_ALLOWED should eval to NULL and any test against NULL will be NULL therefore you should not hit the ROLLBACK. Have you confirmed the trigger works for any low-privilege (i.e. non-sysadmin) logins? I ask because I am wondering if you forgot to include EXECUTE AS 'logon_trigger_login'COMMIT seems like a natural choice to add as the counter-task to ROLLBACK but it doesn't really work that way. A COMMIT has to be paired with an explicit BEGIN TRANSACTION statement whereas ROLLBACK can be issued without being paired with a BEGIN TRANSACTION because when you are inside a trigger it is implied that you are within, at the very least, an implicit transaction. The ROLLBACK serves as a signal that the current operation should be rolled back, whether that be a login attempt, or in the case of a DML trigger, maybe an UPDATE, DELETE or INSERT.</description><pubDate>Fri, 23 Nov 2012 20:18:39 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>OPC, How could I expand on this so that if a login is not in the login table then the login is allowed. Right now if a login is not in that particular table then the login is denied. I imagine I would need to add onto the below code[code="sql"]Use mastergo-- create the logon triggerCREATE TRIGGER connection_limit_trigger ON ALL SERVER    WITH EXECUTE AS 'logon_trigger_login'    FOR LOGONASBEGIN	-- the list of logins to restrict    IF (        SELECT  NUM_ALLOWED        FROM    GK50LIVE.dbo.LimitedLogins        WHERE   LOGIN_ID = ORIGINAL_LOGIN()       ) &amp;lt; (            SELECT  COUNT(*)            FROM    sys.dm_exec_sessions            WHERE   is_user_process = 1                    AND original_login_name = ORIGINAL_LOGIN()           )         BEGIN            ROLLBACK        ENDEND;GO[/code]something like this....[code="sql"]ELSE    COMMITEND[/code]I couldn't find any ways to allow exceptions in BOL. When I put the commit Else....Commit statement in I receive an error.Your suggestions are welcomed. thanks.</description><pubDate>Fri, 23 Nov 2012 14:48:46 GMT</pubDate><dc:creator>kwoznica</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>[quote][b]kwoznica (5/1/2012)[/b][hr]thanks OPC I'm going to put this on my test server now. I'll let you know how it turns out.[/quote]I tested it locally on a 2008R2 instance. Please post back your findings. Thanks.[quote]by the way why do you put the () after ORIGINAL_LOGIN() as if its a function?[/quote]I have to...because it is a function. [u][url=http://msdn.microsoft.com/en-us/library/ms189492(v=sql.105).aspx]ORIGINAL_LOGIN (Transact-SQL)[/url][/u]This works:[code="sql"]SELECT ORIGINAL_LOGIN()[/code]This throws an exception:[code="sql"]SELECT ORIGINAL_LOGIN[/code][quote][font="Courier New"]Msg 207, Level 16, State 1, Line 1Invalid column name 'ORIGINAL_LOGIN'.[/font][/quote]</description><pubDate>Tue, 01 May 2012 15:48:00 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>thanks OPC I'm going to put this on my test server now. I'll let you know how it turns out. by the way why do you put the () after ORIGINAL_LOGIN() as if its a function?</description><pubDate>Tue, 01 May 2012 15:42:50 GMT</pubDate><dc:creator>kwoznica</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>PPS I would also make NUM_ALLOWED an INT to match the data type that COUNT() returns.And one other perfromance bump might be to capture the ORIGINAL_LOGIN() in a variable, instead of calling it twice, like so:[code="sql"]USE masterGOCREATE TRIGGER connection_limit_trigger ON ALL SERVER    WITH EXECUTE AS 'logon_trigger_login'    FOR LOGONASBEGIN    DECLARE @original_login SYSNAME = ORIGINAL_LOGIN();        -- the list of logins to restrict    IF (        SELECT  NUM_ALLOWED        FROM    GK50LIVE.dbo.LimitedLogins        WHERE   LOGIN_ID = @original_login       ) &amp;lt; (            SELECT  COUNT(*)            FROM    sys.dm_exec_sessions            WHERE   is_user_process = 1                    AND original_login_name = @original_login           )         BEGIN            ROLLBACK        ENDEND;GO[/code]</description><pubDate>Tue, 01 May 2012 12:25:58 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>PS I would recommend making LOGIN_ID a SYSNAME and making it the unique clustered index of dbo.LimitedLogins.</description><pubDate>Tue, 01 May 2012 12:21:57 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>You would need to make sure logon_trigger_login can SELECT from the table:[code="sql"]USE GK50LIVE;GOCREATE USER [logon_trigger_login] FROM LOGIN [logon_trigger_login] WITH DEFAULT_SCHEMA = [dbo];GOGRANT SELECT ON dbo.LimitedLogins TO logon_trigger_login;GO[/code]Then you could modify your trigger like this:[code="sql"]USE masterGOCREATE TRIGGER connection_limit_trigger ON ALL SERVER    WITH EXECUTE AS 'logon_trigger_login'    FOR LOGONASBEGIN	-- the list of logins to restrict    IF (        SELECT  NUM_ALLOWED        FROM    GK50LIVE.dbo.LimitedLogins        WHERE   LOGIN_ID = ORIGINAL_LOGIN()       ) &amp;lt; (            SELECT  COUNT(*)            FROM    sys.dm_exec_sessions            WHERE   is_user_process = 1                    AND original_login_name = ORIGINAL_LOGIN()           )         BEGIN            ROLLBACK        ENDEND;GO[/code]</description><pubDate>Tue, 01 May 2012 12:19:43 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>How would I tie back a table such as the one below to the trigger? [code="sql"]USE GK50LIVE;GOSET NOCOUNT ON;USE GK50LIVEgo-- Create LIMITEDLOGINS Table  CREATE TABLE dbo.LimitedLogins   (   Id INT IDENTITY(100,1) PRIMARY KEY   ,Login_Name VARCHAR (50) NOT NULL   ,LOGIN_ID VARCHAR (50) NOT NULL   ,NUM_ALLOWED TINYINT NOT NULL   ,Date_Added Date NULL   );--Add values to the LimitedLogins TableUSE GK50LIVEgoINSERT INTO LimitedLogins(Login_Name , LOGIN_ID, NUM_ALLOWED)VALUES	('James Smith', 'jsmith','1')	,('Joe Kim', 'jkim','1')	,('Gary Harpogian', 'gharpogian','1');	[/code]</description><pubDate>Tue, 01 May 2012 12:03:58 GMT</pubDate><dc:creator>kwoznica</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>[quote][b]Dev (5/1/2012)[/b][hr]I agree with opc.three and would recommend to create a table for logins that you are going to restrict (or any other custom operation in future) for user. Small tweaking in the code though…[code="sql"]SELECT  COUNT(original_login_name) --COUNT(*) FROM sys.dm_exec_sessionsWHERE is_user_process = 1 AND original_login_name = ORIGINAL_LOGIN() ) &amp;gt; 2[/code][/quote]Please do tell...why would you choose COUNT(original_login_name) over COUNT(*) ?</description><pubDate>Tue, 01 May 2012 08:19:11 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>I agree with opc.three and would recommend to create a table for logins that you are going to restrict (or any other custom operation in future) for user. Small tweaking in the code though…[code="sql"]SELECT  COUNT(original_login_name) --COUNT(*) FROM sys.dm_exec_sessionsWHERE is_user_process = 1 AND original_login_name = ORIGINAL_LOGIN() ) &amp;gt; 2[/code]</description><pubDate>Tue, 01 May 2012 01:16:58 GMT</pubDate><dc:creator>Dev</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>You could keep a list of logins hardcoded in the trigger as the example shows, or as I said you would need to keep them in a separate table. Having them in a separate table would allow you to manage the list of logins without recompiling the trigger, but it could turn into a lot more code depending on how many properties you wanted to track in the table, and test for. Whatever you decide, keep the trigger as lightweight as possible.The code sample in the BOL article is actually poorly put together in my opinion. I would setup a login to be used only to execute the trigger. The BOL example tests for login_test in the trigger itself, which does not make sense to me. Try it this way:[code="sql"]USE master;GO-- no one should ever use this login to actually logonto the server. it only exists for purposes of -- executing the login triggerCREATE LOGIN logon_trigger_login WITH PASSWORD = '3KHJ6dhx(0xVYsdf' MUST_CHANGE, CHECK_EXPIRATION = ON;GOGRANT VIEW SERVER STATE TO logon_trigger_login;GO-- create the logon triggerCREATE TRIGGER connection_limit_trigger ON ALL SERVER WITH EXECUTE AS 'logon_trigger_login'FOR LOGONASBEGIN	-- the list of logins to restrict	IF ORIGINAL_LOGIN()  in ('login_test', 'login_test2', 'login_test3', 'login_test4')		AND (	SELECT COUNT(*) 				FROM sys.dm_exec_sessions				WHERE is_user_process = 1 					AND original_login_name = ORIGINAL_LOGIN() ) &amp;gt; 2	BEGIN	    ROLLBACK	ENDEND;GO[/code]</description><pubDate>Mon, 30 Apr 2012 21:24:58 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>If I need to have this trigger take effect on 4 logins would I just add the additional logins as such below. [code="sql"]USE master;GOCREATE TRIGGER connection_limit_triggerON ALL SERVER WITH EXECUTE AS 'login_test'FOR LOGONASBEGINIF ORIGINAL_LOGIN()= 'login_test' OR 'login_test2' OR 'login_test3' OR 'login_test4'    (SELECT COUNT(*) FROM sys.dm_exec_sessions            WHERE is_user_process = 1 AND                original_login_name = 'login_test') &amp;gt; 3    ROLLBACK;END;[/code]</description><pubDate>Mon, 30 Apr 2012 20:33:21 GMT</pubDate><dc:creator>kwoznica</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>That looks like the gist of it, but I am not sure you need to grant anyone VIEW SERVER STATE...all members of public are able to view their own sessions in sys.dm_exec_[b]sessions[/b], so I am not sure you need to use sys.dm_exec_sessions.You'll need to carry some metadata somewhere to list the logins that get kicked out when they exceed the max number of sessions, whether that be in a table in a utility database or hardcoded into the trigger. At the end of the day though, this is a custom feature of the instance so make sure you full document it so it can be properly maintained. Compiling a bad logon trigger can have a major, negative effect on the instance.</description><pubDate>Mon, 30 Apr 2012 11:57:57 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>Would this be the code to use?[code="sql"]USE master;GOCREATE LOGIN login_test WITH PASSWORD = '3KHJ6dhx(0xVYsdf' MUST_CHANGE,    CHECK_EXPIRATION = ON;GOGRANT VIEW SERVER STATE TO login_test;GOCREATE TRIGGER connection_limit_triggerON ALL SERVER WITH EXECUTE AS 'login_test'FOR LOGONASBEGINIF ORIGINAL_LOGIN()= 'login_test' AND    (SELECT COUNT(*) FROM sys.dm_exec_sessions            WHERE is_user_process = 1 AND                original_login_name = 'login_test') &amp;gt; 3    ROLLBACK;END;[/code]</description><pubDate>Mon, 30 Apr 2012 10:28:20 GMT</pubDate><dc:creator>kwoznica</dc:creator></item><item><title>RE: Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>It would be a lot of custom coding, but you may be able to achieve it using a [u][url=http://msdn.microsoft.com/en-us/library/bb326598(v=sql.105).aspx]Server Logon Trigger[/url][/u], depending on exactly what behavior you're trying to restrict. If your requirement is strictly at the Database Level then you may be out of luck. There is no "logon" event that occurs at the database-level. If you just want to limit each Server Login to one connection, then a Logon Trigger can help.</description><pubDate>Mon, 30 Apr 2012 10:15:41 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>Limit Concurrent Logins by database and/or user ID</title><link>http://www.sqlservercentral.com/Forums/Topic1292594-1526-1.aspx</link><description>In my environment I have a sql server 2008 database on 64bit architecture. The production database accessed by users is using sql authentication. Currently I can have one security login mapped to one database user which can log into the database multiple time simultaneously. I have a need to restrict this ability. Can I do so by specific security logins or database users? If so where can I set this feature?If it isnt possible to do this by the security login or database user can it be set at the database level?</description><pubDate>Mon, 30 Apr 2012 09:17:52 GMT</pubDate><dc:creator>kwoznica</dc:creator></item></channel></rss>