I am trying to make a bool property that would toggle the pwdLastSet property.
public bool UserMustChangePassword
{
get { return (long)Entry.Properties["pwdLastSet"].Value == 0; }
set
{
if (value)
{
Entry.Properties["pwdLastSet"].Value = 0;
}
else
{
Entry.Properties["pwdLastSet"].Value = -1;
}
}
}
I can set the property successfully however I cant read the property. I keep getting the following casting error.
System.InvalidCastException: 'Specified cast is not valid.'
Is there a specific way to read this property. I know it may be possible to UserPrincipal
, however I would like to use DirectoryEntry
to keep the code consistent.
Edit: check null before casting
public bool UserMustChangePassword
{
get
{
var value = Entry.Properties["pwdLastSet"].Value;
if (value != null)
return (long)Entry.Properties["pwdLastSet"].Value == 0;
return false;
}
set
{
if (value)
{
Entry.Properties["pwdLastSet"].Value = 0;
}
else
{
Entry.Properties["pwdLastSet"].Value = -1;
}
}
}