C# (CSharp, C-Sharp) Windows Active Directory last logon

January 7, 2017

Hi guys

Please note, many threads were referred in order to compile the script attached below & all I did was re-arranging in order for better reading/formatting

(And I insure that the script works under following scenarios:

  1. You are a domain administrator
  2. You are administrating Windows 2003 onwards Windows domain
  3. Your puter is connected to the domain network ;)

)

Referenced websites/threads

  1. http://stackoverflow.com/questions/15775264/searching-for-lastlogon-attribute-of-user-in-multiple-domain-servers
  2. https://www.codeproject.com/kb/security/lastlogonacrossallwindows.aspx#_comments
  3. http://codebeautify.org/csharpviewer (for formatting the C# code)

How to test the code

Start Visual Studio (I am using 2013 Professional edition, you can use any of the community editions to test the scripts)

Create a new C# Console Application and name it llogon (else you need to change the namespace name “llogon” according to the name you have chosen for your new project.

Add the following references to your project

  1. “Framework -> System.DirectoryServices”
  2. “Browse and add -> C:\Windows\System32\activeds.tlb”

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//Added by Rajesh

//using System.Management;
//using System.Data;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
//http://stackoverflow.com/questions/33274162/the-namespace-of-iadslargeinteger
//ActiveDS namespace
using ActiveDs; // Namespace added via ref to C:\Windows\System32\activeds.tlb
//https://www.codeproject.com/kb/security/lastlogonacrossallwindows.aspx

namespace llogon
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the root entry
            DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
            string configurationNamingContext =
             (string)rootDSE.Properties["configurationNamingContext"].Value;
            string defaultNamingContext =
             (string)rootDSE.Properties["defaultNamingContext"].Value;
            Dictionary<string, Int64> lastLogons = new Dictionary<string, Int64>();
            // Get all the domain controllers
            DirectoryEntry deConfig = new DirectoryEntry("LDAP://" + configurationNamingContext);
            DirectorySearcher dsConfig = new DirectorySearcher(deConfig);
            dsConfig.Filter = "(objectClass=nTDSDSA)";
            foreach (SearchResult srDomains in dsConfig.FindAll())
            {
                DirectoryEntry deDomain = srDomains.GetDirectoryEntry();
                if (deDomain != null)
                {
                    string dnsHostName =
                     deDomain.Parent.Properties["DNSHostName"].Value.ToString();
                    // Get all the users for that domain
                                   DirectoryEntry deUsers =
                     new DirectoryEntry("LDAP://" + dnsHostName + "/" + defaultNamingContext);
                    DirectorySearcher dsUsers = new DirectorySearcher(deUsers);
                    dsUsers.Filter = "(&(objectCategory=person)(objectClass=user))";
                    foreach (SearchResult srUsers in dsUsers.FindAll())
                    {
                        DirectoryEntry deUser = srUsers.GetDirectoryEntry();
                        if (deUser != null)
                        {
                            // Get the distinguishedName and lastLogon for each user
                            // Save the most recent logon for each user in a Dictionary object
                            string distinguishedName = deUser.Properties["distinguishedName"].Value.ToString();
                            Int64 lastLogonThisServer = new Int64();
                            if (deUser.Properties["lastLogon"].Value != null)
                            {
                                IADsLargeInteger lgInt =
                                 (IADsLargeInteger)deUser.Properties["lastLogon"].Value;
                                lastLogonThisServer = ((long)lgInt.HighPart << 32) + lgInt.LowPart;
                            }

                            // Save the most recent logon for each user in a Dictionary object
                            if (lastLogons.ContainsKey(distinguishedName))
                            {
                                if (lastLogons[distinguishedName] < lastLogonThisServer)
                                {
                                    lastLogons[distinguishedName] = lastLogonThisServer;
                                }
                            }
                            else
                            {
                                lastLogons.Add(distinguishedName, lastLogonThisServer);
                            }
                            string readableLastLogon = DateTime.FromFileTime(lastLogonThisServer).ToString();
                            Console.WriteLine("User: " + distinguishedName + "Last logon: " +readableLastLogon);
                        }
                    }
                    Console.ReadLine();
                }
            }

        }
    }
}

Try the code & if you are stuck somewhere, do let me through the comments. I am working on a WPF C# project for a simple Active Directory Reporter / Asset Management System using WMI. Stay tuned & I will soon post the entire solution here :)

regards,

rajesh

 


Using your DVD as a yum repository on a RPM based Linux

November 19, 2016

Frits Hoogland Weblog

So, you’ve just gotten a fresh installed Linux system with Oracle Linux or Redhat Linux from the sysadmin. And with Oracle Linux you can not use the internet (forbidden by company laws is a common one), or you got Redhat Linux and can not use up2date for some reason. Most of the time, when installing Oracle products I am allowed to use the root account myself during the install. The DVD most of the time is still present in the drive.

You could mount the DVD and use ‘rpm’ directly to install packages off the DVD. If you get an error the rpm package has a dependency, you resolve the dependency, if that depended package has a dependency itself, you resolve that, etc. That’s something you could do. But there is an easier way!

Mounting the DVD

The first task is to mount the DVD again. The installation procedure does…

View original post 478 more words


Asking questions, following them up

November 8, 2016

Hello Guys

Not all situations, however for certain interesting cases, I spend loads of time out of my professional and personal life to nail down the concerns, situations you highlight through the comments and I feel neglected and insulted when YOU don’t respond to my queries on timely manner. No, it doesn’t mean I am not going to respond to your specific questions, however I may not follow up the progresses you have made unless I see a keen involvement.

Further, many instances I contact you through the emails provided with your comments and other than 1-2 instances, I have never received replies. I send you emails to insure that your private information are not being visible through the comments area. Hence, please make sure that you will be using regularly used email accounts while commenting or asking questions.

Please follow the below guidelines if you are keen about getting answered

  • Provide me an email address that you are frequently checking
  • Regularly check for the comment replies (You get a notification on email when I reply to your comment)

If I am not supported by YOU as the initiator, please be informed that, I do have better things to do :)

Sorry for the “Attitude”, however, that is going to save me some precious time!

regards,

 

 


Undernet | random nickname and authentication script

October 18, 2016

 

Copy to a text file, rename the file as anyname.mrc and load with mIRC client

–Code

on *:connect:{
  ;If you doubt someone has you in the notify list
  ;the best way to trick them until you authenticate with X (undernet) and mask your IP address
  ;is to pick a random nick, which will overwrite the last nick
  ;you had during last quit!

  if ($network == UnderNet) {
    ;Change according to the server parameters
    ;for authentication
    ;/nick rtkwt
    /nick $rand(a,z) $+ $rand(1,0) $+ $rand(a,z) $+ $rand(a,z)
    /log off
    /set %auth_username $$?=”Enter User Name” | /set %auth_password $$?=”Enter Password”
    /msg x@channels.undernet.org login %auth_username %auth_password
    /unset %auth_username | /unset %auth_password
    clear
    /log on
    ;Clear command above clears the status window, ie, from prying eyes ;)
    ;make sure status window is not logged
    ; /echo $me
    /mode $me +x
    ;To disable the automatic join, you can either delete it or comment it by using a “;” character.
  }
}


Start options in Oracle Forms 12c

October 15, 2016

The simplest, the most effective to start your Forms based application without a browser.

daniel4pitss

So I’ve read quite a lot of blog entries and news regarding Oracle Forms 12c in the last weeks since the release. It is nice to hear and see that Oracle Forms still is not dead.
The release brought some nice features with it. Today I would like to have a look at the different start options, that are now available.

Embedded Java applet

The standard start in the last couple of years. So its still possible to call your form application through the Java plugin embedded in html. Just create a new section in the formsweb.cfg. The parameters for the standard behaviour are:

baseHTML=webutilbase.htm
baseHTMLjpi=webutiljpi.htm

Unbenannt

There is also an option to embed JNLP in html:

basejnlp=base.jnlp
baseHTMLjpi=basejpi_jnlp.htm

Both options require a Java Plugin and a browser.

Java Webstart

This method of starting a Formsapplication is not new. But whats new is the fact that Oracle now supports this option…

View original post 255 more words


It’s been long time!

April 19, 2016

Hello guys!

I know, there were NOT much to post throughout last few months about Oracle as I am nose down into one of the development platforms that I hardly desired for developing business applications.

For me, it was always Oracle forms and reports. The easiest, the most robust development tools for BUSINESS applications which is supported by the ease of SQL, PL/SQL

I am doing loads of research with C# & SQL database, which will be the main technologies behind our next proposed ERP suite. After spending almost 15 years with Forms and reports, I don’t see Oracle is too keen about modernizing their most loved development tools & the latest release lacks loads of features those would have helped to reclaim the desktop based business applications.

Wondering whether there is still room for desktop applications, especially for businesses? dude! there is, and there will be!

 

So stay tuned, I will start posting my “findings” about C#, who knows may be some sample applications using Windows forms or WPF & Oracle database in near future!

regards,

rajesh

 


Line Drilldown in GL Journal Screen Requires User Name and Password

February 11, 2016

Oracle Application World

Problem
One of my clients was complaining about error “You Have Insufficient Privileges For The Current Operation” while Line Drill down from GL Journal screen. Internet explorer version was 6.0.2900 with Java 6 update 21. Oracle suggestion in METALINK note “1106048.1“ is Uninstall all updates on Java 6 which are more than 15. What does it means, Java version should be Java 6 with Update 15 maximum. Downgrading Java is a problem as might some other Application requires latest Java Version. It’s always hectic to explain to the client with business reason. So this option out of scope. Later on I found out that issue is because of one option only and no need to uninstall all updates.
Solution
1. On the local computer, move to the Control Panel – choosing Start/Settings/Control Panel – Choose the Java icon
2. A window opens – choose the advanced tab
3. Locate the…

View original post 33 more words