Account Expiration with System.DirectoryServices.AccountManagement

Posted on 2011-05-10
Last Modified: 2012-05-11
Hi, I'm creating an acount using the System.DirecotryServices.AccountMangement. All works except for the expiration date on the accounts does not reflect what typed into the textbox. Below is the code. How can I get the account expiration date onto the account. Thanks.

PrincipalContext AddContext = new PrincipalContet(ContextType.Domain, "", context);
        UserPrincipal usr = new UserPrincipal(AddContext, newuser, "password", true);
        usr.GivenName = fn;
        usr.MiddleName = mi;
        usr.Surname = ln;
        usr.SamAccountName = newuser;
        usr.UserPrincipalName = newuser + "";
        usr.AccountExpirationDate = Convert.ToDateTime(txtVarDate.Text);
Question by:centem
    LVL 19

    Accepted Solution

    Could it be the UTC issue?


    As with all DateTime properties in System.DirectoryServices.AccountManagement, the time returned is in UTC form. To convert it to local time, call the ToLocalTime method on the return object.

    When you set the AccountExpirationDate, the time will default to UTC. If you want to write in local time, then specify the Kind property of your DateTime object as Local().

    See about using ToLocalTime, etc.
    LVL 29

    Expert Comment

    by:Paul Jackson
    Be aware that the datetime stored on the account will be in UTC form. What you are doing in the above code looks fine.

    Author Comment

    Thank you very much. Although its behind by two days. For example, when I set it to 2012-05-05 I actually get 2012-05-03. How can I fix that? Thanks.
    LVL 29

    Expert Comment

    by:Paul Jackson
    This article should help to explain the discrepancy :

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Mapping Drives using Group policy preferences Are you still using old scripts to map your network drives if so this article will show you how to get away for old scripts and move toward Group Policy Preference for mapping them. First things f…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
    This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now