Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to get the Expiration Date of an Active Directory User
WhatsApp
Abhay Shanker
May 03
2015
5.8
k
0
0
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.DirectoryServices;
using
Microsoft.VisualBasic;
namespace
AD
{
class
Program
{
static
public
void
Main(
string
[] args)
{
DirectoryEntry user =
new
DirectoryEntry(
"LDAP://CN="
+ username +
","
+
" OU= Web Users,DC=SERVERNAME,DC=com"
,
null
,
null
, AuthenticationTypes.Secure);
DateTime expires = DateTime.FromFileTime(GetInt64(user,
"accountExpires"
));
string
expDate = expires.ToShortDateString();
Console.WriteLine(expDate.ToString());
string
exp = expires.ToLongDateString();
Console.WriteLine(exp.ToString());
Console.WriteLine(expires.ToString());
Console.ReadKey();
}
static
string
username =
"testADuser1"
;
static
public
Int64 GetInt64(DirectoryEntry entry,
string
attr)
{
//we will use the marshaling behavior of
//the searcher
DirectorySearcher ds =
new
DirectorySearcher(
entry,
String.Format(
"({0}=*)"
, attr),
new
string
[] {attr},
SearchScope.Base);
SearchResult sr = ds.FindOne();
if
(sr !=
null
)
{
if
(sr.Properties.Contains(attr))
{
return
(Int64) sr.Properties[attr][0];
}
}
return
-1;
}
}
}
AD User expire date
AD
Active dirctory
Up Next
How to get the Expiration Date of an Active Directory User