[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Password Last Set Time incorrect - Active Directory

Posted on 2011-04-27
9
Medium Priority
?
883 Views
Last Modified: 2012-05-11
I have a method that allows me to set a users active directory password by passing in their username as a variable. after setting the password, i am checking to see if more than 5 minutes have passed before allowing another password reset.
if(DateTime.Now.Subtract(PasswordLastSet).TotalMinutes > 5)

Open in new window


this is where my issue occurs. after adding a break point and running debug, i noticed that my password last set time, is 5 hours in the future. how can i correct this?

Cheers,
Brendan
public void ModifyUser(string username)
        {
            string sPwd = SetSecurePassword();
            
            DirectoryEntry entry = GetDirectoryEntry();
            DirectorySearcher search = new DirectorySearcher(entry);
            search.Filter = "(SAMAccountName=" + username + ")";

            SearchResult sResult = search.FindOne();
            if (sResult != null)
            {
                try
                {
                    DirectoryEntry updateEntry = sResult.GetDirectoryEntry();
                    updateEntry.Invoke("SetPassword", new object[] { sPwd });
                    updateEntry.CommitChanges();
                    updateEntry.Close();
                    passWord = sPwd.ToString();

                }


                catch (Exception ex)
                {
                    lblErrorMessage.Text = ex.ToString();
                }
            }

Open in new window

0
Comment
Question by:brendanlefavre
  • 5
  • 3
9 Comments
 
LVL 16

Expert Comment

by:Kamal Khaleefa
ID: 35481209
make sure when you update your password you are inserting the correct time to the database
also make sure your machine and the server(active directory) are having the same corrct time
0
 

Author Comment

by:brendanlefavre
ID: 35484828
I'm using ActiveDirectoryServices.AccountManage to return a user principal object. when i look at the LastPasswordSet property that is returned, it shows that it's using UTC instead of local time. This would explain the 5 hour difference.

How can I configure my app to work around this?
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35485649
Hi, did you try DateTime.ToLocalTime() method?:

if(DateTime.Now.Subtract(PasswordLastSet.ToLocalTime()).TotalMinutes > 5)

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:brendanlefavre
ID: 35487203
i'm stll getting the time returned as UTC when I use the .ToLocalTime as suggested.

I am displaying the results using
lblPasswordLastSet.Text = PasswordLastSet.ToString();

Open in new window


I have added the code that I am using to return the PassWordLastSet object
public UserPrincipal GetUser(string sUserName)
    {
        PrincipalContext oPrincipalContext = GetPrincipalContext();

        UserPrincipal oUserPrincipal =
           UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
        if (oUserPrincipal != null)
        {
            BuildUser(oUserPrincipal);
        }
        return oUserPrincipal;
    }

        private void BuildUser(UserPrincipal user)
    {
        //Populate the user with items available in the UserPrincipal object
        if (user != null)
        {
            if (user.LastPasswordSet.HasValue)
            this.PasswordLastSet = (DateTime)user.LastPasswordSet;
        }
    }

Open in new window

0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 35487338
Well... then try with:
if(DateTime.UtcNow.Subtract(PasswordLastSet).TotalMinutes > 5)

Open in new window


DateTime.UtcNow
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35487458
And if that fails, then try this (the problem is I dont know if really your LastPasswordSet var has an Utc time zone defined):
if(DateTime.UtcNow.Subtract(DateTime.SpecifyKind(PasswordLastSet, DateTimeKind.Utc)).TotalMinutes > 5)

Open in new window

0
 

Author Closing Comment

by:brendanlefavre
ID: 35487466
Thank you for your assistance.

Your solution helped me achieve exactly what I was trying to accomplish.

It makes more sense to just check the total time against UTC instead of converting it to local time. This way if there are users across multiple time zones, the results will be the same.

cheers,
Brendan
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35487471
You can get that info by using:
string infoPasswordLastSet = new DateTimeOffset(PasswordLastSet).ToString();

Open in new window


It will return a date time string with it time zone offset.
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35487481
Good to know bro
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

829 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