繁体   English   中英

将所有用户限制为1个会话

[英]limit all users to 1 session

我被要求将SQL Server配置为每次登录只允许一个会话。 我已经找到了一些关于创建登录触发器的引用,以防止登录建立多个会话,但我想知道是否有某种方法可以在较低级别定义它,所以这个会话限制是默认的,而不是而不是必须在每个用户的另一个登录中定义它?

我在stackoverflow上的“可能已经有你的答案的问题”和“类似的问题”中看到了很多关于这个主题的引用,但到目前为止还没有找到或者没有理解描述我想要做什么的帖子。 我还看到了一个关于声明式管理框架的参考资料,它允许您按照我认为的策略配置SQL Server。

我将继续在这里查看文章以尝试学习这一点,但在此期间......非常感谢建议!

联机丛书中登录触发器的示例与我认为您想要的非常接近,我做了一些更改,使其适用于所有登录。

-- Trigger must be created by a user with 'view server state' permission in order the trigger to have unrestricted access to sys.dm_exec_sessions.
create trigger connection_limit_trigger on all server with execute as self for logon
as
begin
    -- Check whether the caller is a SysAdmin.
    -- Note: The trigger is executing under an elevated security context so we must switch to the caller's context to test their SysAdmin status.
    declare @IsSysAdmin int
    execute as caller
    set @IsSysAdmin = isnull(is_srvrolemember ('sysadmin'), 0)
    revert

    -- If the user is not a SysAdmin and there are already too many non-dormant sessions for this login then 'rollback' the logon.
    -- Note: sys.dm_exec_sessions does not include an entry for this logon attempt.
    if ((@IsSysAdmin = 0) and ((select count(1) from sys.dm_exec_sessions where is_user_process = 1 and status <> 'Dormant' and original_login_name = original_login()) > 1))
    begin
        raiserror('This login has exceeded the maximum of 1 connections to this SQL Server.', 16, 1)
        rollback
    end
end

我添加了一个检查,因此该限制不适用于SysAdmin登录,也不计算休眠连接池连接。 有几点需要注意;

  • 如果连接没有正确关闭,它可以在sys.dm_exec_sessions中暂停一段时间,在这种情况下,用户将无法重新连接,直到死连接自行清除。
  • 如果你弄乱了登录触发器,你可以将自己(以及其他所有人!)锁定在SQL Server之外。 有关如何返回的详细信息,请参阅登录触发器页面:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM