brendanlefavre
asked on
Password Last Set Time incorrect - Active Directory
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.
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
if(DateTime.Now.Subtract(PasswordLastSet).TotalMinutes > 5)
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();
}
}
ASKER
I'm using ActiveDirectoryServices.Ac countManag e 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?
How can I configure my app to work around this?
Hi, did you try DateTime.ToLocalTime() method?:
if(DateTime.Now.Subtract(PasswordLastSet.ToLocalTime()).TotalMinutes > 5)
ASKER
i'm stll getting the time returned as UTC when I use the .ToLocalTime as suggested.
I am displaying the results using
I have added the code that I am using to return the PassWordLastSet object
I am displaying the results using
lblPasswordLastSet.Text = PasswordLastSet.ToString();
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;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
ASKER
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
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
You can get that info by using:
It will return a date time string with it time zone offset.
string infoPasswordLastSet = new DateTimeOffset(PasswordLastSet).ToString();
It will return a date time string with it time zone offset.
Good to know bro
also make sure your machine and the server(active directory) are having the same corrct time