Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Logon Trigger to control Sessions

$
0
0

Good afternoon!

Please forgive my English mistakes.

I'm trying to control number of connections or session form every user on a instance server in SQL Server Express 2008R2 and 2012 but it doesn't work as I want.

First i used the link http://msdn.microsoft.com/es-es/library/bb326598%28v=sql.110%29.aspx

I wrote the next code:

USE MASTER
GO
CREATE TRIGGER test
ON ALL SERVER
WITH ENCRYPTION
FOR LOGON
AS
BEGIN
IF
  ORIGINAL_LOGIN()= SYSTEM_USER
  AND
  (
    SELECT
      COUNT(*)
    FROM
      [SYS].[DM_EXEC_SESSIONS]
    WHERE
      [IS_USER_PROCESS] = 1
      AND [ORIGINAL_LOGIN_NAME] = SYSTEM_USER
  ) > 1
  BEGIN
    ROLLBACK;
  END;
END;

But I locked all connections EVEN MINE! ooops.

I restored the connectios Successfuly and I read more about when i ran the select in Management it returned more than one connection and I suspect that one is for object explorer and the other is for the sql script.

If I run the next script with the user sa and loggin with the Management Studio I obtain as a result 2 connections:

SELECT
  COUNT(*)
FROM
  [SYS].[DM_EXEC_SESSIONS]
WHERE
  [IS_USER_PROCESS] = 1
  AND [ORIGINAL_LOGIN_NAME] = SYSTEM_USER

If I run Management Studio whitout connection and just use the Database Engine Query I obtain a different result show just one connection.

If I use sa user and open 5 query windows and I can see the correct result.

As I see I have 5 connections one for each Sql Query window that's right! BUT THE TRIGGER DOESN'T DO ITS WORK! I HAVE MORE CONNECTION THAN SPECIFIED IN THE TRIGGER!

I read that the number of connections may be different because some tasks in management studio and the services running.

But if I use a test user with less permissions than sa I have different results because  Sql Server shows me only one connection but I have Object Explorer and Query window opened the same as the user sa and if I open more Query Windows sql returns me always just 1 connection.

I openned 9 Query windows but SQL server returns me just 1 connection. I'M GETTING CRAZY AND I DON'T UNDERSTAND WHY AND WHY THE TRIGGER DOESN'T DO ITS WORK!

I tried this varying code with the test user:

SELECT
  COUNT(*)
FROM
  [SYS].[DM_EXEC_SESSIONS] AS [DS] WITH(NOLOCK) INNER JOIN
    [SYS].[DM_EXEC_CONNECTIONS] AS [DC] WITH(NOLOCK) ON
      [DS].[SESSION_ID] = [DC].[SESSION_ID]
WHERE
  [DS].[IS_USER_PROCESS] = 1
  AND [DS].[ORIGINAL_LOGIN_NAME] = SYSTEM_USER

But I have the result that the user hasn't permission to do this acction.

I modified the trigger with the next code:

USE MASTER;
GO
CREATE TRIGGER test
ON ALL SERVER
WITH ENCRYPTION, EXECUTE AS 'sa'
FOR LOGON
AS
BEGIN
IF
  ORIGINAL_LOGIN() = SYSTEM_USER
  AND
  (
    SELECT
      COUNT([DS].[SESSION_ID])
    FROM
      [SYS].[DM_EXEC_SESSIONS] AS [DS] WITH(NOLOCK) INNER JOIN
        [SYS].[DM_EXEC_CONNECTIONS] AS [DC] WITH(NOLOCK) ON
          [DS].[SESSION_ID] = [DC].[SESSION_ID]
    WHERE
      [DS].[IS_USER_PROCESS] = 1
      AND [DS].[ORIGINAL_LOGIN_NAME] = SYSTEM_USER
      AND [DS].[ORIGINAL_LOGIN_NAME] <> 'sa'
  ) > 1
  BEGIN
    ROLLBACK;
  END;
END;

But it doesn't work because I can open various Sql Query windows and Management (One normal and one as Manager and one directly on the server as normal user and as Manager) 4 connections with the user test.

I tried to upload immages but the page says: "Body text cannot contain images or links until we are able to verify your account." can you tell me what must I do too.

Please tell me what am I doing wrong.

Thanks a lot for your help.


Viewing all articles
Browse latest Browse all 23857

Trending Articles