0

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; 
        }
    }                                                                                   
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dblock247
  • 6,167
  • 10
  • 44
  • 66

1 Answers1

0

You need to check its Count property to make sure there is a value. Try this,

if (Entry.Properties["pwdLastSet"].Count > 0)
{
    return (Entry.Properties["pwdLastSet"][0] == 0)
}
else 
    return false;

Edit:

Seems the problem comes from that you are querying Properties of DirectoryEntry instead of SearchResult. See this question. I have a copy of working code that is also querying SearchResult.

kennyzx
  • 12,845
  • 6
  • 39
  • 83
  • I think the problem is the casting type. The count > 0. – Dblock247 Feb 14 '18 at 01:48
  • 1
    A System.DirectoryServices.DirectoryEntry instance does not have a pwdLastSet property. Have to get a SearchResultCollection and then loop through each SearchResult instance, and then get the pwdLastSet property, right? – kennyzx Feb 14 '18 at 02:13
  • No I am setting it just fine. The issue is getting the value. It maybe be something to do with AD INTEGER8 types to .NET Int64. see third post here: https://bytes.com/topic/visual-basic-net/answers/370827-getting-pwdlastset-attrib-value-though-net – Dblock247 Feb 14 '18 at 02:36
  • I don't know what the issue was but I found out that setting the passwordExpired flag does the same thing and I already have ways to use bitwise operators to set the flags. @kennyzx thanks for your help – Dblock247 Feb 14 '18 at 03:08
  • Okay, np, it should be a simple issue of missing value and/or invalid type casting, glad you have found a workaround. – kennyzx Feb 14 '18 at 03:12
  • Just for any one who see this I think I was awrong about the passwordExpired flag. Athough it should do what I expected I believe it can only be set by the system. Every time I change it when a save the object it removes the flag. So instead I use a method called ExpirePasswordNow() (Yea I stole the name from UserPrinciple). Where I can just set the property = 0 which sets the flags. – Dblock247 Feb 14 '18 at 13:50