【问题标题】:When I try to add user exception: The server is unwilling to process the request. and setPassword exception: thrown by the target of an invocation当我尝试添加用户异常时:服务器不愿意处理请求。和 setPassword 异常:由调用的目标抛出
【发布时间】:2016-02-19 02:36:32
【问题描述】:

我尝试像下面那样做,得到 “服务器不愿意处理请求。”因为这条线newUser.Invoke("Put", new object[] { "userAccountControl", "512" }); 我评论了它,它的工作,但发现新的异常“由调用的目标抛出”来自这个newUser.Invoke("SetPassword", new object[] { model.Password });

在此之前我使用window server 2003R2(VM) 到Active Directory 没有问题。现在我用window server 2012R2有问题,为什么?

     ConnectAD con = new ConnectAD();
                DirectoryEntry de = con.GetConnection();
                Utility ut = new Utility();

                using (DirectorySearcher searcher = new DirectorySearcher(de))
                {
                    searcher.Filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))", model.UserName);
                    using (SearchResultCollection resultUser = searcher.FindAll())
                    {
                        bool DoesExistUsesr = resultUser.Count > 0;
                        if (!DoesExistUsesr)
                        {//User doesn't exist
                            searcher.Filter = string.Format("(&(objectClass=organizationalUnit)(ou={0}))", model.Group);
                            using (SearchResultCollection resultOU = searcher.FindAll())
                            {
                                bool DoesExistOU = resultOU.Count > 0;
                                if (DoesExistOU)
                                {//OU does exist
                                    de.Path = ut.SetChildPath("OU=" + model.Group);//"LDAP://" + Properties.Settings.Default.domainMyAD + "/" + "OU=" + model.Group + "," + Properties.Settings.Default.pathMyAD;
                                    DirectoryEntry newUser = de.Children.Add("CN=" + model.UserName, "user");
                                    newUser.Properties["displayName"].Value = model.Name + " " + model.Surname;
                                    newUser.Properties["givenName"].Value = model.Name;
                                    newUser.Properties["sn"].Value = model.Surname;
                                    newUser.Properties["mail"].Value = model.Email;
                                    newUser.Properties["department"].Value = model.Department;
                                    newUser.Properties["title"].Value = model.Title;
                                    newUser.Properties["userPrincipalName"].Value = model.UserName + "@" + Properties.Settings.Default.domainMyAD;
                                    newUser.Properties["sAMAccountname"].Value = model.UserName;
                                    newUser.Properties["PwdLastSet"].Value = -1;
                                    newUser.CommitChanges(); 
                                    newUser.Invoke("SetPassword", new object[] { model.Password }); //<-- ***thrown by the target of an invocation***
                                    newUser.Invoke("Put", new object[] { "userAccountControl", "512" });
                                    newUser.CommitChanges();
                                }....

提前致谢。

内部异常:密码不符合密码策略要求。检查最小密码长度, 密码复杂性和密码历史要求 - 错误代码:800708c5

【问题讨论】:

    标签: c# visual-studio-2013 active-directory


    【解决方案1】:

    您不能启用没有密码的帐户(这是将 userAccountControl 设置为 512 所做的)。您必须在保存新帐户后执行此操作。

    但您也不需要使用 Invoke 来设置 userAccountControl 属性。在第一次 CommitChanges 之后试试这个:

    ...
    //newUser.Invoke("Put", new object[] { "userAccountControl", "512" }); <-- Remove this
    newUser.CommitChanges();
    newUser.Invoke("SetPassword", model.Password);
    newUser.Properties["userAccountControl"].Value = 512;
    newUser.CommitChanges();
    

    【讨论】:

    • 怎么样?它在我每天创建新帐户的代码中完美运行。
    • 异常“被调用的目标抛出”如何解决这个问题。我理解 512 使用密码启用然后异常“服务器不愿意处理请求。”
    • 在第一次 CommitChanges 之前不要尝试更新 userAccountControl。
    • 它不起作用,调用的目标已抛出异常。设置密码行
    • 我编辑我的代码和你一样,它不起作用“异常已被调用的目标抛出”newUser.Invoke("SetPassword", model.Password);
    【解决方案2】:

    我解决了。关于密码复杂性(大写、小写、长度)的内部异常。 这种情况下,我测试使用密码 123 创建帐户。我是 AD 的新手 :(

    【讨论】:

      猜你喜欢
      • 2011-11-23
      • 2012-03-11
      • 2020-11-11
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 2013-07-24
      • 2021-09-17
      • 2015-05-17
      相关资源
      最近更新 更多