Step one: Fire up SSMS and create a New Database Called UserLogin (or whatever you want to name it) assign appriate permissions.
Step two: Create two different tables. Name the first one Logon and the second one Logoff.
Add the following Columns to the tables. UserName,Hostname,IP,DateTime and Event
Step three: Create a gpo to run logon.exe and logoff.exe. Under user configuration goto windows settings\scripts in the logon\logoff enter the path to run the files. IE domain.local\netlogon\logon.exe
Step Four: Open Visual studio and create a console application.
using System;using System.Net;using System.Data;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;
namespace LogonLogoff{ class Program { public static string UserName { get; } public static string ip { get; } public static string hostname = Dns.GetHostName(); static void Main(string[] args) { string connString = @"Server =sqlserver.domain.local; Database = Login; User ID=MyLogin;Password=MyPassword";
String UserName = Environment.UserName; string Hostname = Dns.GetHostName(); String IP = Dns.GetHostByName(Hostname).AddressList[1].ToString(); //1 string Event = "Logon"; //change this.. Console.WriteLine(); Console.WriteLine("UserName: {0}", UserName); Console.WriteLine("Computer Name: {0}", Hostname); Console.WriteLine("IP Address: {0}", IP); Console.WriteLine("Logon Time: {0}", DateTime.Now);
SqlConnection connection = new SqlConnection(connString); SqlCommand command = connection.CreateCommand(); try {//3 command.CommandText = "Insert Into Logon (UserName,Hostname,IP,Date,Event) Values (@Username,@Hostname,@IP,@DateTime,@Event)";
command.Parameters.AddWithValue("@Username", UserName); command.Parameters.AddWithValue("@Hostname", Hostname); command.Parameters.AddWithValue("@IP", IP); command.Parameters.AddWithValue("@DateTime", DateTime.Now); command.Parameters.AddWithValue("@Event", Event); connection.Open();
command.ExecuteNonQuery();
connection.Close();
} catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); throw; } Environment.Exit(0); }
}} Save this binary as logon.exe Change the string event to logoff (or whatever you want that event to be called) and the connection string to the logoff table. build and rename to logoff.exe or whatever you called it. now i know you can pass arguments to the application and i will get into that a latter date. for now this is quick and light weight and easy to go. And finally go into SSRS and you can create a report to show the information.
namespace LogonLogoff{ class Program { public static string UserName { get; } public static string ip { get; } public static string hostname = Dns.GetHostName(); static void Main(string[] args) { string connString = @"Server =sqlserver.domain.local; Database = Login; User ID=MyLogin;Password=MyPassword";
String UserName = Environment.UserName; string Hostname = Dns.GetHostName(); String IP = Dns.GetHostByName(Hostname).AddressList[1].ToString(); //1 string Event = "Logon"; //change this.. Console.WriteLine(); Console.WriteLine("UserName: {0}", UserName); Console.WriteLine("Computer Name: {0}", Hostname); Console.WriteLine("IP Address: {0}", IP); Console.WriteLine("Logon Time: {0}", DateTime.Now);
SqlConnection connection = new SqlConnection(connString); SqlCommand command = connection.CreateCommand(); try {//3 command.CommandText = "Insert Into Logon (UserName,Hostname,IP,Date,Event) Values (@Username,@Hostname,@IP,@DateTime,@Event)";
command.Parameters.AddWithValue("@Username", UserName); command.Parameters.AddWithValue("@Hostname", Hostname); command.Parameters.AddWithValue("@IP", IP); command.Parameters.AddWithValue("@DateTime", DateTime.Now); command.Parameters.AddWithValue("@Event", Event); connection.Open();
command.ExecuteNonQuery();
connection.Close();
} catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); throw; } Environment.Exit(0); }
}} Save this binary as logon.exe Change the string event to logoff (or whatever you want that event to be called) and the connection string to the logoff table. build and rename to logoff.exe or whatever you called it. now i know you can pass arguments to the application and i will get into that a latter date. for now this is quick and light weight and easy to go. And finally go into SSRS and you can create a report to show the information.