Getting a local Windows user login timestamp in C #

I am trying to inspect various .NET class libraries for some, where I can get a registered user of the local machine, either connected to the domain or not. Till

System.Security.Principal.WindowsPrincipal LoggedUser = System.Threading.Thread.CurrentPrincipal as 
System.Security.Principal.WindowsPrincipal;
// This returns the username
LoggedUser.Identity.Name

This will return the username, however, is there a way to get session information, what you see in AD or logged in, session duration, etc. user context, actions such as workstation locked, basiclly user presence.

If you have any ideas, it will be very appreciated. Thanks in advance.

+3
source share
2 answers

Active Directory , LDAP, System.DirectoryServices. , .

, .

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace ADMadness
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectorySearcher search = new DirectorySearcher("LDAP://DC=my,DC=domain,DC=com");
            search.Filter = "(SAMAccountName=MyAccount)";
            search.PropertiesToLoad.Add("lastLogonTimeStamp");


            SearchResult searchResult = search.FindOne();


            long lastLogonTimeStamp = long.Parse(searchResult.Properties["lastLogonTimeStamp"][0].ToString());
            DateTime lastLogon = DateTime.FromFileTime(lastLogonTimeStamp);


            Console.WriteLine("The user last logged on at {0}.", lastLogon);
            Console.ReadLine();
        }
    }
}
+2
+1

Source: https://habr.com/ru/post/1705346/


All Articles