Click here to Skip to main content
15,881,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
good night thank you for your cooperation, I do not speak English but am familiar with the forum and is very good. Could help me with my problem. I have an application that allows users to change passwords of domain created in c # works perfectly but now I must abide by the policies of the company where it is implemented. I need users to change their password but a different and not the same (security policies). I do it with this code, which works but I can put the same key without validating the password history used. I can not use a database to store user passwords. The code below:
C#
private void resetearContraseñaEnPdc(string usuario, string scontraseña)
    {
        Forest forest = Forest.GetForest(forestContext); 
        string pdc = forest.SchemaRoleOwner.Name.ToString(); 
        object ocontraseña = scontraseña;

       
            DirectoryEntry entry = conexldap.conexionConControladorVariable(pdc);//aqui me conecto con el directorio activo. 
            DirectorySearcher search = new DirectorySearcher(entry);
            search.Filter = "sAMAccountName=" + usuario;
            result = search.FindOne();          

        
        if (result != null)
        {
            try
            {
                                               
                entry = result.GetDirectoryEntry();
                entry.Invoke("SetPassword", ocontraseña);
                entry.CommitChanges();
                entry.Close();
            }
            catch(Exception ex)
            {
                
                throw new Exception("La contraseña no cumple con las políticas de seguridad, la clave debe contener mayúsculas, números o caracteres especiales.");
            }
        }
        entry.Close();

    }


Thank you very much.
Posted
Updated 18-Jul-12 23:10pm
v2
Comments
Christian Graus 18-Jul-12 20:45pm    
I'm not sure I understand the question. You can write code to apply the rules they want applied, but you can't tell if the password they enter has been used before ?

1 solution

Edit:
You could also use
C#
entry.Invoke("ChangePassword", new[] {oldpassword, newpassword});

Of course this requires requesting the current password, but my understanding is it will throw an exception if password policy is not met, where "SetPassword" is more like an administrative function.

Original:

Might want to take a look at this. However this appears to only be able to execute on Server 2003+ so not sure if this would work for you or not.

NetValidatePasswordPolicy
http://msdn.microsoft.com/en-us/library/aa370661.aspx[^]
 
Share this answer
 
v2
Comments
johanbustos 1-Aug-12 11:40am    
good morning Thanks for responding. I tell you, try using the code you placed: entry.Invoke ("ChangePassword", new [] {oldpassword, newpassword}); but does not work, it generates an exception. It says no more reasons: There Was an exception in the target invocation. Thank you. This is the code I have now:

try
{
DirectoryEntry entry = conexldap.conexionDirectorioActivoUsuarioNormal(pdc, usuario, "Calidad123"); //SE CREA UNA VARIABLE DE TIPO DirectoryEntry A LA QUE SE LE ASIGNARA LA CONEXION AL DIRECTORIO ACTIVO DEL METODO ANTERIOR
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "sAMAccountName=" + usuario;
result = search.FindOne();



throw new Exception("Se presento un error al cambiar la contraseña.");


if (result != null)
{

string viejo = "Calidad123";
string nuevo = "Prueba1234"

entry = result.GetDirectoryEntry();

entry.Invoke("ChangePassword", new Object[] { viejo, nuevo });

//entry.Invoke("setPassword", ocontraseña);

entry.CommitChanges();
entry.Close();
}


}catch(Exception ex)
{

throw new Exception("La contraseña no cumple con las políticas de seguridad, la clave debe contener mayúsculas, números o caracteres especiales.");
}
}
Trak4Net 2-Aug-12 13:20pm    
Does the exception have an inner exception? I read someone else was getting this error and the inner exception had to do with password complexity policies, password history etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900