locked
Getuser account expiry date in c# RRS feed

  • Question

  • Dear Exprts,

    Would any one can assist me to develop very simple code for AD object  to user name, i had google and found many links but still confuse, your help appriciated...

     

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


    namespace GetADuser
    {
        class Program
        {
            static void Main(string[] args)
            {
                public bool IsUserExpired(string sUserName)
     {
     UserPrincipal oUserPrincipal = GetUser(sUserName);
     if (oUserPrincipal.AccountExpirationDate != null)
     {
     return false;
     }
     else
     {
     return true;
     }
     }


            }
        }
    }


    Support@Mytechnet.me

    Tuesday, January 8, 2013 6:06 PM

Answers

  • Hi MyTechnet,

    If you want to get account expiry date, you can check below code which come from this thread:Retrieve Active Directory Accounts Expire Date Using C#

          PrincipalContext context = new PrincipalContext(ContextType.Domain);
          UserPrincipal p = UserPrincipal.FindByIdentity(context, "Domain\\User Name");
          if (p.AccountExpirationDate.HasValue)
          {
            DateTime expiration = p.AccountExpirationDate.Value.ToLocalTime();
          }

    Good day.

    Bob Shen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Proposed as answer by Bob Shen Wednesday, January 16, 2013 5:28 AM
    • Marked as answer by Bob Shen Thursday, January 17, 2013 5:20 AM
    • Edited by Bob Shen Tuesday, January 22, 2013 3:17 AM
    Wednesday, January 9, 2013 9:15 AM