2013-10-04 343 views
1

有沒有辦法獲得連接的IP地址,而用戶不在SysAdmin角色或被授予VIEW SERVER STATE?其中之一是發出此查詢所需:在沒有VIEW SERVER STATE權限的情況下在SQL Server中獲取連接的IP地址?

select client_net_address from sys.dm_exec_connections where session_id = @@spid 

我想記錄內的INSTEAD-OF觸發器的IP地址,但我不想去授予查看服務器狀態的權限給所有用戶。

感謝您的任何建議。


後續2013-10-05: 感謝您的各種意見。向所有用戶授予VIEW SERVER STATE有哪些危險?

回答

-1
CREATE PROCEDURE Some_Proc 
WITH EXECUTE AS 'User_With_Permission' 
AS 
BEGIN 
    SET NOCOUNT ON; 
     select client_net_address from sys.dm_exec_connections where session_id = @@spid 
END 

調用此PROC從觸發

+1

除非模塊簽署這仍然產生錯誤15562或數據庫設置爲值得信賴。因此,將其封裝在存儲過程中是一個額外且不必要的步驟。 –

+0

即使我們授予GRANT ** IMPERSONATE在登錄:: [login_to_be_impersonated]到[login1]; ** –

+0

您是否嘗試過?你可以包含一些演示它的代碼嗎? –

1

您可以設定觸發執行爲業主:

ALTER TRIGGER dbo.whatever_instead 
ON dbo.whatever 
WITH EXECUTE AS OWNER 
INSTEAD OF INSERT 
AS 
BEGIN 
    SET NOCOUNT ON; 

    INSERT dbo.log_table(column1, ...) 
    SELECT client_net_address, ... 
    FROM sys.dm_exec_connections 
    WHERE session_id = @@SPID; 

    ... other stuff here I'm sure ... 
END 
GO 

然而,默認情況下,你應該得到以下錯誤:

Msg 15562, Level 16, State 1, Procedure whatever_instead
The module being executed is not trusted. Either the owner of the database of the module needs to be granted authenticate permission, or the module needs to be digitally signed. The statement has been terminated.

解決此問題的一種方法是將數據庫設置爲TRUSTWORTHY

ALTER DATABASE database_name SET TRUSTWORTHY ON; 

不要輕易採取這種設置:

+0

謝謝。看起來我需要TRUSTWORTHY ON才能使用CLR UDF。 –

相關問題