Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Account Expiration with System.DirectoryServices.AccountManagement

Posted on 2011-05-10
Medium Priority
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, "domain.com", context);
        UserPrincipal usr = new UserPrincipal(AddContext, newuser, "password", true);
        usr.GivenName = fn;
        usr.MiddleName = mi;
        usr.Surname = ln;
        usr.SamAccountName = newuser;
        usr.UserPrincipalName = newuser + "@domain.com";
        usr.AccountExpirationDate = Convert.ToDateTime(txtVarDate.Text);
Question by:centem
  • 2
LVL 20

Accepted Solution

Daniel Van Der Werken earned 2000 total points
ID: 35730122
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
ID: 35730133
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

ID: 35730962
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
ID: 35733823
This article should help to explain the discrepancy : http://www.rlmueller.net/AccountExpires.htm

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

577 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