Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Limit Concurrent Logins by database and/or user ID Expand / Collapse
Author
Message
Posted Monday, April 30, 2012 9:17 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 3:31 PM
Points: 182, Visits: 420
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?
Post #1292594
Posted Monday, April 30, 2012 10:15 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:08 PM
Points: 7,097, Visits: 12,599
It would be a lot of custom coding, but you may be able to achieve it using a Server Logon Trigger, 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.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1292643
Posted Monday, April 30, 2012 10:28 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 3:31 PM
Points: 182, Visits: 420
Would this be the code to use?

USE master;
GO
CREATE LOGIN login_test WITH PASSWORD = '3KHJ6dhx(0xVYsdf' MUST_CHANGE,
CHECK_EXPIRATION = ON;
GO
GRANT VIEW SERVER STATE TO login_test;
GO
CREATE TRIGGER connection_limit_trigger
ON ALL SERVER WITH EXECUTE AS 'login_test'
FOR LOGON
AS
BEGIN
IF ORIGINAL_LOGIN()= 'login_test' AND
(SELECT COUNT(*) FROM sys.dm_exec_sessions
WHERE is_user_process = 1 AND
original_login_name = 'login_test') > 3
ROLLBACK;
END;


Post #1292656
Posted Monday, April 30, 2012 11:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:08 PM
Points: 7,097, Visits: 12,599
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_sessions, 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.


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1292698
Posted Monday, April 30, 2012 8:33 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 3:31 PM
Points: 182, Visits: 420
If I need to have this trigger take effect on 4 logins would I just add the additional logins as such below.

USE master;
GO

CREATE TRIGGER connection_limit_trigger
ON ALL SERVER WITH EXECUTE AS 'login_test'
FOR LOGON
AS
BEGIN
IF 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') > 3
ROLLBACK;
END;

Post #1292911
Posted Monday, April 30, 2012 9:24 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:08 PM
Points: 7,097, Visits: 12,599
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:

USE master;
GO
-- no one should ever use this login to actually logonto the server. it only exists for purposes of
-- executing the login trigger
CREATE LOGIN logon_trigger_login WITH PASSWORD = '3KHJ6dhx(0xVYsdf' MUST_CHANGE, CHECK_EXPIRATION = ON;
GO
GRANT VIEW SERVER STATE TO logon_trigger_login;
GO

-- create the logon trigger
CREATE TRIGGER connection_limit_trigger ON ALL SERVER
WITH EXECUTE AS 'logon_trigger_login'
FOR LOGON
AS
BEGIN
-- 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() ) > 2
BEGIN
ROLLBACK
END
END;
GO



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1292918
Posted Tuesday, May 1, 2012 1:16 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 6:53 PM
Points: 2,013, Visits: 1,589
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…

SELECT  COUNT(original_login_name) --COUNT(*) 
FROM sys.dm_exec_sessions
WHERE is_user_process = 1
AND original_login_name = ORIGINAL_LOGIN() ) > 2



Dev

Devendra Shirbad | BIG Data Architect / DBA | Ex-Microsoft CSS (SQL 3T)
*** Open Network for Database Professionals ***

LinkedIn: http://www.linkedin.com/in/devendrashirbad
Post #1292954
Posted Tuesday, May 1, 2012 8:19 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:08 PM
Points: 7,097, Visits: 12,599
Dev (5/1/2012)
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…

SELECT  COUNT(original_login_name) --COUNT(*) 
FROM sys.dm_exec_sessions
WHERE is_user_process = 1
AND original_login_name = ORIGINAL_LOGIN() ) > 2


Please do tell...why would you choose COUNT(original_login_name) over COUNT(*) ?


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1293176
Posted Tuesday, May 1, 2012 12:03 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 3:31 PM
Points: 182, Visits: 420
How would I tie back a table such as the one below to the trigger?

USE GK50LIVE;
GO

SET NOCOUNT ON;
USE GK50LIVE
go

-- 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 Table
USE GK50LIVE
go
INSERT INTO LimitedLogins
(Login_Name , LOGIN_ID, NUM_ALLOWED)
VALUES
('James Smith', 'jsmith','1')
,('Joe Kim', 'jkim','1')
,('Gary Harpogian', 'gharpogian','1');


Post #1293374
Posted Tuesday, May 1, 2012 12:19 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:08 PM
Points: 7,097, Visits: 12,599
You would need to make sure logon_trigger_login can SELECT from the table:

USE GK50LIVE;
GO
CREATE USER [logon_trigger_login] FROM LOGIN [logon_trigger_login] WITH DEFAULT_SCHEMA = [dbo];
GO
GRANT SELECT ON dbo.LimitedLogins TO logon_trigger_login;
GO

Then you could modify your trigger like this:

USE master
GO
CREATE TRIGGER connection_limit_trigger ON ALL SERVER
WITH EXECUTE AS 'logon_trigger_login'
FOR LOGON
AS
BEGIN
-- the list of logins to restrict
IF (
SELECT NUM_ALLOWED
FROM GK50LIVE.dbo.LimitedLogins
WHERE LOGIN_ID = ORIGINAL_LOGIN()
) < (
SELECT COUNT(*)
FROM sys.dm_exec_sessions
WHERE is_user_process = 1
AND original_login_name = ORIGINAL_LOGIN()
)
BEGIN
ROLLBACK
END
END;
GO



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1293388
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse