首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DirectoryEntry SetPassword方法返回访问拒绝异常

DirectoryEntry SetPassword方法返回访问拒绝异常
EN

Stack Overflow用户
提问于 2017-10-13 08:45:33
回答 1查看 2K关注 0票数 2

在asp.net MVC应用程序中,我在试图使用directoryEntry.Invoke重置密码时遇到了访问被拒绝的错误。

试图更改密码的用户访问该页,并在IIS中标记SSL requiredClient Certificates - Required

相关守则:

代码语言:javascript
运行
复制
directoryEntry.Invoke("SetPassword", new object[] { model.Password });
                directoryEntry.Properties["LockOutTime"].Value = 0;
                directoryEntry.Close();

准确的错误是-

代码语言:javascript
运行
复制
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
   --- End of inner exception stack trace ---
   at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)

Web.config -

代码语言:javascript
运行
复制
<authentication mode="Windows" />
    <identity impersonate="false" />
    <authorization>
      <deny users="?" />
    </authorization>
  • 应用程序池运行在一个AD帐户下;也是本地管理组[Domain1\AppPoolUser]的一部分。
  • 应用程序请求用户证书。
  • 试图更改密码[Domain2\testUser]和应用程序池运行的帐户的用户位于不同的域中,但这不太可能是一个问题。AppPoolUser的有效权限允许ChangePassword访问testUser帐户。
  • 我甚至尝试在与测试帐户相同的用户帐户下运行应用程序池,但是它没有改变任何东西。

已经在网上查过了,但我不清楚问题可能是什么。我所看到的最密切相关的事情是- ACCESSDENIED))

但是,正如在我的例子中提到的,应用程序池是在有限的技术帐户下运行的,我认为SSL证书没有任何问题。

  1. 是否需要为AD中的应用程序池帐户请求委托控制?
  2. 或者有可能是我错过的另一个问题。
EN

回答 1

Stack Overflow用户

发布于 2017-10-15 01:13:17

我们有类似的要求更改或重置密码。我们使用下面的代码片段。

代码语言:javascript
运行
复制
        /// <summary>
        /// Resets the user password.
        /// </summary>        
        public static void ResetUserPassword(string domain, string domainUsername, string domainPassword, string sAMAccountName, string newPassword,
            bool askToChangePassword, bool unlockAccount, bool passRespectDomainPolicy, bool superuser)
        {
            // Get root directory entry
            using (var entry = GetDirectoryEntry(domain, domainUsername, domainPassword, AuthenticationTypes.Secure))
            {
                var displayName = string.Empty;

                // Search for the user with the same sAMAccountName
                using (var searcher = new DirectorySearcher(entry))
                {
                    // Filter results by SAMAccountName
                    searcher.Filter = string.Format("(SAMAccountName={0})", sAMAccountName);

                    // Search and return only one result
                    var result = searcher.FindOne();

                    // Check if result is returned
                    if (result == null) throw new Exception("Could not find user: " + sAMAccountName);

                    // Get the user directory entry
                    var userEntry = result.GetDirectoryEntry();

                    // Read name value
                    if (userEntry.Properties.Contains("displayName") && userEntry.Properties["displayName"].Count > 0)
                        displayName = Convert.ToString(userEntry.Properties["displayName"][0]);

                    // Validate password
                   // string errorMessage;
                   // if (passRespectDomainPolicy &&
                     //   !IsValidPassword(domain, sAMAccountName, newPassword, displayName, userEntry, superuser, out errorMessage))
                    // {
                      //  if (!string.IsNullOrEmpty(errorMessage)) throw new Exception(errorMessage);
                      //  throw new Exception("Password is not valid as per AD policy. Please consult Administrator.");
                    // }

                    // Earlier we used impersonation to reset password on same DC.
                    // But that didn't worked and so removed.
                    userEntry.Invoke("SetPassword", newPassword);

                    // 0(for on) and -1(for off) for LDAP case. For WinNT it is opposite.
                    // Set "Ask to change password at next login"
                    if (askToChangePassword)
                        userEntry.Properties["pwdLastSet"].Value = 0;

                    // Unlock account if required
                    if (unlockAccount)
                        userEntry.Properties["lockoutTime"].Value = 0;

                    // Commit changes
                    userEntry.CommitChanges();
                }
            }
        }

值得注意的一点是,我们正在根目录条目using (var entry = GetDirectoryEntry(domain, domainUsername, domainPassword, AuthenticationTypes.Secure)){的上下文中运行代码using (var entry = GetDirectoryEntry(domain, domainUsername, domainPassword, AuthenticationTypes.Secure)){

我的意思是,entry表示持有域管理员用户和密码的对象。此管理员用户必须具有更改AD.的完全权限。

让我们知道你的测试结果。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46725933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档