I have problem setting the password for an ADAM user using C#. I used
the SetPassword code given in the Microsoft page, changed several
parameters, but ran into an exception. I wonder if other people have
solved this problem. But I have searched Google groups for a couple
days but still couldn't get a solution.
I copied the code from:
http://msdn.microsoft.com/library/de...r_password.asp
Then when the program reaches the first call of:
objUser.Invoke("SetOption", ...);
It throws the an exception with the following information:
Message:
Exception has been thrown by the target of an invocation.
BaseException:
Exception from HRESULT: 0x80005008.
Inner Exception:
System.Runtime.InteropServices.COMException (0x80005008):
Exception from HRESULT: 0x80005008.
Source:
mscorlib
StackTrace:
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags
invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers,
Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags
invokeAttr, Binder binder, Object target, Object[] args,
ParameterModifier[] modifiers, CultureInfo culture, String[]
namedParameters)
at System.Type.InvokeMember(String name, BindingFlags invokeAttr,
Binder binder, Object target, Object[] args)
at System.DirectoryServices.DirectoryEntry.Invoke(Str ing
methodName, Object[] args)
at WindowsApplication1.Form1.button1_Click(Object sender, EventArgs
e) in c:\setpassword\windowsapplication1\form1.cs:line 153
System.Object InvokeDispMethod(System.String,
System.Reflection.BindingFlags,
TargetSite:
System.Object, System.Object[], Boolean[], Int32, System.String[])
For your information, here is the code I have:
<-- code begins -->
private void button1_Click(object sender, System.EventArgs e)
{
const long ADS_OPTION_PASSWORD_PORTNUMBER = 6;
const long ADS_OPTION_PASSWORD_METHOD = 7;
const int ADS_PASSWORD_ENCODE_REQUIRE_SSL = 0;
const int ADS_PASSWORD_ENCODE_CLEAR = 1;
AuthenticationTypes AuthTypes; // Authentication flags.
int intPort; // Port for instance.
DirectoryEntry objUser; // User object.
string strPath; // Binding path.
string strPort; // Port for instance.
string strServer; // Server for instance.
string strUser; // User DN.
// Construct ADsPath binding string.
// Change "localhost" to appropriate server.
// Change "389" to LDAP port appropriate for instance,
// or use SSL port (default = "636") for secure connection.
// Change "CN=TestUser,O=Fabrikam,C=US" to DN of user.
strServer = "[MY_LDAP_SERVER]";
strPort = "389";
strUser = "[A_USER_DISTINGUISHED_NAME]";
strPath = String.Concat("LDAP://", strServer,
":", strPort, "/", strUser);
Debug.WriteLine("Bind to: " + strPath);
// Set authentication flags.
// For non-secure connection, use LDAP port and
// ADS_USE_SIGNING |
// ADS_USE_SEALING |
// ADS_SECURE_AUTHENTICATION
// For secure connection, use SSL port and
// ADS_USE_SSL | ADS_SECURE_AUTHENTICATION
AuthTypes = AuthenticationTypes.Signing |
AuthenticationTypes.Sealing |
AuthenticationTypes.Secure;
// Bind to user object using LDAP port.
try
{
objUser = new DirectoryEntry(
strPath, "[LDAP_USER_NAME]", "[LDAP_USER_PASSWORD", AuthTypes);
objUser.RefreshCache();
}
catch (Exception e)
{
Debug.WriteLine("Error: Bind failed.");
Debug.WriteLine(" " + e.Message);
return;
}
// Set port number, method, and password.
intPort = Int32.Parse(strPort);
try
{
// Note: A password should normally
// not be entered in code,
// but should be obtained from the user interface.
objUser.Invoke("SetOption", new object[]
{ADS_OPTION_PASSWORD_PORTNUMBER, intPort}); <-- **Exception thrown
here**
objUser.Invoke("SetOption", new object[]
{ADS_OPTION_PASSWORD_METHOD,
ADS_PASSWORD_ENCODE_CLEAR});
objUser.Invoke("SetPassword", new object[]
{"goodpassword"});
}
catch (Exception e)
{
Debug.WriteLine("Error: Set password failed.");
Debug.Indent();
Debug.WriteLine(e.Message);
Debug.WriteLine("base exception\n" + e.GetBaseException().Message);
Debug.WriteLine("inner exception\n" + e.InnerException);
Debug.WriteLine("source\n" + e.Source);
Debug.WriteLine("StackTrace\n" + e.StackTrace);
Debug.WriteLine("TargetSite\n" + e.TargetSite);
return;
}
Debug.WriteLine("Success: Password set.");
return;
}
<-- code ends -->
Thank you very much and I appreciate anyone who has any suggestions!