954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with getting lastlogon for account - C#.NET / AD

Hi guys!
I have a code:

String Lastlogon(string username, string domain)
        {
            try
            {
                {
                    DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain,domain);
                    DateTime latestLogon = DateTime.MinValue;
                    string servername = null;
                    DomainControllerCollection dcc = DomainController.FindAll(context);
                    foreach (DomainController dc in dcc)
                    {
                        DirectorySearcher ds;
                        using (dc)
                        using (ds = dc.GetDirectorySearcher())
                        {
                            ds.Filter = String.Format("(sAMAccountName={0})",username);
                            ds.PropertiesToLoad.Add("lastLogon");
                            ds.SizeLimit = 1;
                            SearchResult sr = ds.FindOne();
                            if (sr != null)
                            {
                                DateTime lastLogon = DateTime.MinValue;
                                if (sr.Properties.Contains("lastLogon"))
                                {
                                    lastLogon = DateTime.FromFileTime((long)sr.Properties["lastLogon"][0]);
                                }
                                if (DateTime.Compare(lastLogon, latestLogon) > 0)
                                {
                                    latestLogon = lastLogon;
                                    servername = dc.Name;
                                }
                            }
                        }
                    }
                    return latestLogon.ToString();
                }
            }
            catch (Exception)
            {
                err = true;
                return null;
            }
        }


as a domain I provide "woobie.net"
as an user I provide Login "jlennon"

Unfortunately I always receive a message "server not operational"
Anyone knows what can be wrong with that code?

VasquezPL
Junior Poster
120 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

check these links might be helpful

this like ,... this link ,..... this link ,....and this link

abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
 

Hmmh..
From what I have read...Everything is ok...
I have checked the DNS server with IPCONFIG..and its correct :/

I have admin rights to extract everything I want...
Only this parameter is problematic

VasquezPL
Junior Poster
120 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: