private long CalculateUserAccountControl() { long userAccCtrl = 512; foreach (string option in OptionsList) { int i = ListUserOptions.Items.IndexOf(option); if (ListUserOptions.GetItemChecked(i)) { switch (i) { case 0: pwdLastSet = "0"; break; case 1: userAccCtrl += 64; pwdLastSet = ConvertToUnixTimestamp(DateTime.Now).ToString(); break; case 2: userAccCtrl += 65536; break; case 3: userAccCtrl += 128; break; case 4: userAccCtrl += 2; break; case 5: userAccCtrl += 262144; break; case 6: userAccCtrl += 1048576; break; case 7: userAccCtrl += 2097152; break; case 8: userAccCtrl += 4194304; break; } } } return(userAccCtrl); }
private void FillUserOptions() { ListUserOptions.Items.AddRange(OptionsList); foreach (string option in OptionsList) { int i = ListUserOptions.Items.IndexOf(option); switch (i) { case 0: ListUserOptions.SetItemChecked(i, bMustChangePwd); break; case 1: ListUserOptions.SetItemChecked(i, bUserCannotChange); break; case 2: ListUserOptions.SetItemChecked(i, bNeverExpiresPwd); break; case 3: ListUserOptions.SetItemChecked(i, bStorePwd); break; case 4: ListUserOptions.SetItemChecked(i, bAcountDisable); break; case 5: ListUserOptions.SetItemChecked(i, bSmartCardRequired); break; case 6: ListUserOptions.SetItemChecked(i, bAccSensitive); break; case 7: ListUserOptions.SetItemChecked(i, bUseDESDescription); break; case 8: ListUserOptions.SetItemChecked(i, bNotKrbAuthentication); break; } } }
/// <summary> /// Modifies the specified attributes for the selected AD Object either "user" to AD Schema template /// </summary> /// <returns></returns> public bool OnApply() { List <LDAPMod> ldapAttrlist = new List <LDAPMod>(); List <LDAPMod> attrlist = new List <LDAPMod>(); if (dirnode == null || String.IsNullOrEmpty(dirnode.DistinguishedName) || dirnode.LdapContext == null) { return(true); } if (ListUserOptions.GetItemChecked(0) && ListUserOptions.GetItemChecked(1)) { string Msg = "You cannot select both 'User must change passowrd at next logon' and 'User cannot change password'\nfor the same user"; MessageBox.Show(this, Msg, CommonResources.GetString("Caption_Console"), MessageBoxButtons.OK, MessageBoxIcon.Information); ListUserOptions.SetItemChecked(1, false); return(false); } if (ListUserOptions.GetItemChecked(0) && ListUserOptions.GetItemChecked(2)) { string Msg = "You have selected 'Password never expires'. \nThe user will not be required to change the password at next logon."; MessageBox.Show(this, Msg, CommonResources.GetString("Caption_Console"), MessageBoxButtons.OK, MessageBoxIcon.Information); ListUserOptions.SetItemChecked(0, false); return(false); } //the following portion of code uses openldap "ldap_Modify_s" string basedn = dirnode.DistinguishedName; DirectoryContext dirContext = dirnode.LdapContext; string[] objectClass_values = null; if (Logonname != null && !(Logonname.Trim().Equals(txtlogon.Text.Trim()))) { objectClass_values = new string[] { txtlogon.Text.Trim(), null }; LDAPMod attr = new LDAPMod((int)LDAPMod.mod_ops.LDAP_MOD_REPLACE, "userPrincipalName", objectClass_values); attrlist.Add(attr); } if (txtpreLogonname.Text.Trim().Length > 0 && !(PreLogonname.Trim().Equals(txtpreLogonname.Text.Trim()))) { objectClass_values = new string[] { txtpreLogonname.Text.Trim(), null }; LDAPMod attr = new LDAPMod((int)LDAPMod.mod_ops.LDAP_MOD_REPLACE, "sAMAccountName", objectClass_values); attrlist.Add(attr); } if (dateTimePicker.Enabled && dateTimePicker.Value != null) { objectClass_values = new string[] { ConvertToUnixTimestamp(dateTimePicker.Value).ToString(), null }; LDAPMod attr = new LDAPMod((int)LDAPMod.mod_ops.LDAP_MOD_REPLACE, "accountExpires", objectClass_values); attrlist.Add(attr); } if (!String.IsNullOrEmpty(pwdLastSet)) { objectClass_values = new string[] { pwdLastSet, null }; LDAPMod attr = new LDAPMod((int)LDAPMod.mod_ops.LDAP_MOD_REPLACE, "pwdLastSet", objectClass_values); attrlist.Add(attr); } //userWorkstations attribute if (String.IsNullOrEmpty(sUserWorkStations)) { objectClass_values = new string[] { null } } ; else { objectClass_values = new string[] { sUserWorkStations, null } }; LDAPMod attri = new LDAPMod((int)LDAPMod.mod_ops.LDAP_MOD_REPLACE, "userWorkstations", objectClass_values); attrlist.Add(attri); if (ListUserOptions.SelectedIndices.Count > 0) { objectClass_values = new string[] { CalculateUserAccountControl().ToString(), null }; LDAPMod attr = new LDAPMod((int)LDAPMod.mod_ops.LDAP_MOD_REPLACE, "userAccountControl", objectClass_values); attrlist.Add(attr); } LDAPMod[] attrArry = attrlist.ToArray(); int ret = -1; if (attrArry != null && attrArry.Length != 0) { ret = dirContext.ModifySynchronous(basedn, attrArry); } else { return(true); } if (ret != 0) { string sMsg = ErrorCodes.LDAPString(ret); container.ShowError(sMsg); return(false); } else { DirectoryEntry de = new DirectoryEntry(dirnode.DistinguishedName); de.Properties["pwdLastSet"].Value = pwdLastSet; de.CommitChanges(); } return(true); }