<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Logon Trigger - Techyaz.com</title>
	<atom:link href="https://techyaz.com/tag/logon-trigger/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Tips, Tutorials and How-to Topics</description>
	<lastBuildDate>Mon, 14 May 2018 15:15:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>

<image>
	<url>https://techyaz.com/wp-content/uploads/2017/11/cropped-Site-icon-150x150.png</url>
	<title>Logon Trigger - Techyaz.com</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Create a Logon Trigger to Restrict sysadmin logins to connect to SQL Server for given Time Interval</title>
		<link>https://techyaz.com/sql-server/t-sql/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/</link>
					<comments>https://techyaz.com/sql-server/t-sql/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/#comments</comments>
		
		<dc:creator><![CDATA[Maruti Nandan]]></dc:creator>
		<pubDate>Mon, 21 Aug 2017 07:58:44 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[HowTO]]></category>
		<category><![CDATA[logins]]></category>
		<category><![CDATA[Logon Trigger]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=566</guid>

					<description><![CDATA[<p>I was struggling with unwanted modifications of SQL Server configurations by random SQL Server sysadmin logins who were part of our SQL Server Instance. Every time we cannot monitor what has been changed and who are doing it on your&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/">Create a Logon Trigger to Restrict sysadmin logins to connect to SQL Server for given Time Interval</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I was struggling with unwanted modifications of SQL Server configurations by random SQL Server sysadmin logins who were part of our SQL Server Instance. Every time we cannot monitor what has been changed and who are doing it on your non-prod servers. We generally monitor our production boxes for any unwanted activities. here i will show you how to create a Logon trigger to restrict sysadmin logins to not perform such activities. I will also show you how we can restrict them between a given time interval. Access will be allowed during office hours and restricted during out of office hours.</p>
<h3><span style="color: #000080;"><strong>Logon trigger Overview</strong></span></h3>
<p><strong>Logon Triggers</strong> fire in response to a LOGON event. It fires after the authentication phase of logging in finishes, but before the user session is actually established. Logon Triggers are very useful in tracking and restricting login events.</p>
<p>Sometimes few applications require sysadmin privilege to run and if you downgrade this access, application stops working. So, we cannot restrict sysadmin logins of such applications. There is always a scope of security breach if another login has sysadmin access except DBAs on SQL Server Instance.</p>
<p>To fix this issue, I have created a logon trigger that will restrict access of such application logins who are <strong>sysadmin </strong>on your SQL Server Instance<strong>. </strong> We can restrict them to access SQL Server directly through SSMS or any other medium. You need to pass that medium into code just like I have passed Management studio.</p>
<h3><span style="color: #000080;"><strong>Logon Trigger T-SQL Code</strong></span></h3>
<p>Below is the Logon Trigger T-SQL code which will be used to restrict any login for given time frame.</p>
<pre class="brush: sql; title: ; notranslate" title=""><strong><span class="kwrd" style="color: blue;">
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE trigger [logon_rest]
on all server for logon
as begin
declare @Program varchar(128)
declare @systemname varchar(128)
select @Program =PROGRAM_NAME,@systemname=HOST_NAME from sys.dm_exec_sessions as A
where a.session_id=@@SPID
if ORIGINAL_LOGIN() in('sa') </span><span class="kwrd" style="color: green;">/**Login For Which Access Need to restricted **/</span><span class="kwrd" style="color: blue;">
and @Program like'%Management%studio' </span><span class="kwrd" style="color: green;">/** Program via access restricted **/</span><span class="kwrd" style="color: blue;">
and
(GETDATE()&gt;(dateadd(day, datediff(day, 0, getdate()), 0) + '18:00') </span><span class="kwrd" style="color: green;">/**Last time till access allowed **/</span><span class="kwrd" style="color: blue;">
or
GETDATE()&lt;(dateadd(day, datediff(day, 0, getdate()), 0) + '09:00')) </span><span class="kwrd" style="color: green;">/**Start time from access allowed **/</span><span class="kwrd" style="color: blue;">
begin
Raiserror ('This is out of office hour',1,1)
rollback;
end
end;
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ENABLE TRIGGER [logon_rest] ON ALL SERVER
GO
</span></strong></pre>
<h5><span style="color: #000080;"><strong>Testing</strong></span></h5>
<p>For the testing purpose, I have taken sa account as a potential application login. You can use your identified login which are running with sysadmin rights. Even we can restrict such logins to connect to SQL Server during given time frame.</p>
<p>Here we have disabled sa server level login to connect through SSMS Before 9:00 AM and Post 6:00 PM. That means server login sa cannot connect to SQL Server Instance between given time slot. You can alter the time slot as per your convenience.</p>
<p>Just for example we have tried to login on SQL server through SSMS between given time frame and here you can see the output. I am not able to connect to SQL Server anymore. You can see this in below screenshot. But at the same time you can login to the same SQL Server Instance using other sysadmin or normal login accounts which is available in sys.syslogins.</p>
<p><img fetchpriority="high" decoding="async" class="aligncenter size-full wp-image-567" src="http://techyaz.com/wp-content/uploads/2017/08/1-sa-login.jpg" alt="logon trigger" width="644" height="488" srcset="https://techyaz.com/wp-content/uploads/2017/08/1-sa-login.jpg 644w, https://techyaz.com/wp-content/uploads/2017/08/1-sa-login-300x227.jpg 300w" sizes="(max-width: 644px) 100vw, 644px" /></p>
<p>Even we can track the <a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15434-not-drop-login-user-currently-logged/" target="_blank" rel="noopener">failed login</a> attempt made on this SQL Server Instance once you can make a database connection using another login. You can check the SQL Server error logs or you can just run <strong>sp_readerrorlog</strong> stored procedure to display errors. You can see our test login attempt is showing in below screenshot on the server.</p>
<p><img decoding="async" class="aligncenter size-full wp-image-568" src="http://techyaz.com/wp-content/uploads/2017/08/2-errorlog.jpg" alt="" width="607" height="215" srcset="https://techyaz.com/wp-content/uploads/2017/08/2-errorlog.jpg 607w, https://techyaz.com/wp-content/uploads/2017/08/2-errorlog-300x106.jpg 300w" sizes="(max-width: 607px) 100vw, 607px" /></p>
<p><span style="color: #800000;"><em><strong>Read more about SQL Server Login Issue related Articles:</strong></em></span></p>
<ul>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15173/" target="_blank" rel="noopener">How to fix Error 15173: Revoke the permissions before dropping the login.</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15138-error-3729/" target="_blank" rel="noopener">Fix error 15138 &amp; Error 3729: DROP login failed</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15434-not-drop-login-user-currently-logged/" target="_blank" rel="noopener">How to fix error 15434: Could not drop login as the user is currently logged in</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15170/" target="_blank" rel="noopener">Fixing error 15170: Login owns one or more SQL Agent Jobs</a></strong></li>
</ul>
<p>I hope you like this article. Please follow us on our <a href="https://www.facebook.com/Techyaz/">facebook page</a> and on <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/">Create a Logon Trigger to Restrict sysadmin logins to connect to SQL Server for given Time Interval</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
