Friday, June 24, 2011

Get the user Expire Time From Active Directory using User Name

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 exp1 = expires.ToShortDateString();
            string exp=expires.ToLongDateString();

        static string username = "testuser1";
        static public Int64 GetInt64(DirectoryEntry entry, string attr)
            //we will use the marshaling behavior of
            //the searcher
            DirectorySearcher ds = new DirectorySearcher(
                String.Format("({0}=*)", attr),
                new string[] { attr },

            SearchResult sr = ds.FindOne();

            if (sr != null)
                if (sr.Properties.Contains(attr))
                    return (Int64)sr.Properties[attr][0];
            return -1;


Note: DC=SERVERNAME in this place please give you server name like 'myserver1'

Featured Post

Manage Deployment Slots in App Service using Azure CLI

Manage Deployment Slots in App Service using Azure CLI az  az -h  az group list --output table resource_group=Regroup_5wlAgklxKkjC6 location...

Popular posts