日期:2015-03-14  浏览次数:5141 次

Question
登录进行投票
0
登录进行投票
Hi everybody; I have the following code to set a new password to a existing user account at the domain: 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
 
namespace testads
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryEntry de = new DirectoryEntry("LDAP://DOMAIN", "user", "password", AuthenticationTypes.Secure);
 
            DirectorySearcher ds = new DirectorySearcher(de);
 
            string UserName = "user2";
 
            ds.Filter = "(samaccountname=" + UserName + ")";
            ds.SearchScope = SearchScope.Subtree;
            SearchResult results = ds.FindOne();
 
            if (results != null)
            {
               
                DirectoryEntry users = de.Children.Add(UserName, "User");
                
                users.Invoke("SetPassword", new object[] {"NewPassword123"});  <--- Here it's the Exception ---->
 
                users.CommitChanges();
                de.Close();
                users.Close();
 
             }
           
            
        }
    }
}
 
 
But I get the following exception: 
 
"Exception has been thrown by the target of an invocation."
 
andthe inner exception said: InnerException {"An invalid dn syntax has been specified.\r\n"} System.Exception {System.DirectoryServices.DirectoryServicesCOMException}; those anybody have an idea that what can I do???
 
Please some help. 
 
Thanks to everybody!!!!
 
2009年9月18日 19:48
回复 | 引用
JeanOsorio 的头像
JeanOsorioAdvantel (Partner) 5 分数
答案
Question
登录进行投票
0
登录进行投票
Hmmm ok... please look at this link http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/a6c4e5d7-4265-4503-ace3-7513a61be564 hope that it helps ;)
With great code, comes great complexity, so keep it simple stupid...
已建议为答案 Paw Jershauge 2009年9月18日 21:12
已标记为答案 JeanOsorio 2009年9月21日 19:17
2009年9月18日 20:43
回复 | 引用
Paw Jershauge 的头像
Paw Jershauge545 分数
全部回复
Question
登录进行投票
0
登录进行投票
I've seen this before, try this instead:
 
users.Invoke("SetPassword", "NewPassword123");
Dont cast the new password as an object array.
With great code, comes great complexity, so keep it simple stupid...
已标记为答案 JeanOsorio 2009年9月21日 19:16
取消答案标记 JeanOsorio 2009年9月21日 19:17
2009年9月18日 20:26
回复 | 引用
Paw Jershauge 的头像
Paw Jershauge545 分数
 Question
登录进行投票
0
登录进行投票
I get the same Exception :S 
2009年9月18日 20:28
回复 | 引用
JeanOsorio 的头像
JeanOsorioAdvantel (Partner) 5 分数
 Question
登录进行投票
0
登录进行投票
Hmmm ok... please look at this link http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/a6c4e5d7-4265-4503-ace3-7513a61be564 hope that it helps ;)
With great code, comes great complexity, so keep it simple stupid...
已建议为答案 Paw Jershauge 2009年9月18日 21:12
已标记为答案 JeanOsorio 2009年9月21日 19:17
2009年9月18日 20:43
回复 | 引用
Paw Jershauge 的头像
Paw Jershauge545 分数
 Question
登录进行投票
0
登录进行投票
I still have this problem, any help please.
2009年9月21日 13:27
回复 | 引用
JeanOsorio 的头像
JeanOsorioAdvantel (Partner) 5 分数
 Question
登录进行投票
0
登录进行投票
Hmmm same error ???? 
 
Does your app run administrative rights/ domain admin ????
With great code, comes great complexity, so keep it simple stupid...
2009年9月21日 13:28
回复 | 引用
Paw Jershauge 的头像
Paw Jershauge545 分数
 Question
登录进行投票
0
登录进行投票
Yes, the user account have administrative rights, to change password and propertiers.
2009年9月21日 14:12
回复 | 引用
JeanOsorio 的头像
JeanOsorioAdvantel (Partner) 5 分数
 Question
登录进行投票
0
登录进行投票
This is the new error:
 
The object already exists.
 
InnerException: NULL
2009年9月21日 14:20
回复 | 引用
JeanOsorio 的头像
JeanOsorioAdvantel (Partner) 5 分数
 Question
登录进行投票
1
登录进行投票
Hmm ok.. could you post your code ??? to see if somethings missing...
With great code, comes great complexity, so keep it simple stupid...
已标记为答案 JeanOsorio 2009年9月21日 19:16
取消答案标记 JeanOsorio 2009年9月21日 19:16
2009年9月21日 17:12
回复 | 引用
Paw Jershauge 的头像
Paw Jershauge545 分数
 Question
登录进行投票
0
登录进行投票
Paw, I can solve the problem, here is the code. Thanks for your help :D
 
using
 
 
 
System;
 
using
 
 
 
System.Collections.Generic;
 
using
 
 
 
System.Linq;
 
using
 
 
 
System.Text;
 
using
 
 
 
System.DirectoryServices;
 
namespace
 
 
 
testads
 
{
 
 
 
class Program
 
{
 
 
 
static void Main(string[] args)
 
{
 
 
 
DirectoryEntry de = new DirectoryEntry(LDAP://DOMAIN, "user", "password", AuthenticationTypes.Secure);
 
 
 
object obj = de.NativeObject;
 
 
 
DirectorySearcher ds = new DirectorySearcher(de);
 
 
 
string UserName = "user2";
 
ds.Filter =
 
"(samaccountname=" + UserName + ")";
 
ds.SearchScope =
 
SearchScope.Subtree;
 
 
 
SearchResult results = ds.FindOne();
 
 
 
if (results != null)
 
{
 
 
 
#region
 
 
 
SetPassword
 
 
 
DirectoryEntry rag = (DirectoryEntry)results.GetDirectoryEntry();
 
 
 
object error = rag.Invoke("setPassword", new object[] { "SuperPassword" });
 
rag.CommitChanges();
 
de.Close();
 
rag.Close();
 
#endregion
 
 
}
 
}
 
}
 
}
 
2009年9月21日 19:16
回复 | 引用
JeanOsorio 的头像
JeanOsorioAdvantel (Partner) 5 分数
 Question
登录进行投票
0
登录进行投票
Hi 
JeanOsorio
,
 
Check following  this coding,
 
string userPassword = "1234qwer$";
 
string connectionPrefix = "LDAP://" +  DOMAIN + "/" + connPrefix; // ( connPrefix--> CN=Users,DC=Domian without .com,DC=COM ) 
 DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
                DirectoryEntry newUser = dirEntry.Children.Add ("CN=" + firstName, "user");
newUser.Invoke("SetPassword", new object[] { userPassword });
 
                newUser.CommitChanges();
                dirEntry.Close();
                newUser.Close();
已建议为答案 ColourInteligent 2009年9月25日 11:53
2009年9月25日 11:52
回复 | 引用
ColourInteligent 的头像
ColourInteligent0 分数
 Question
登录进行投票
0
登录进行投票
Does your Domain have a "Minimum Password Age" set via GPO? I've found that this error gets thrown if the change request is being made inside the the Minimum password age.