【问题标题】:DirectoryEntry SetPassword method returns Access Denied ExceptionDirectoryEntry SetPassword 方法返回访问被拒绝异常
【发布时间】:2017-10-13 08:45:33
【问题描述】:

在一个 asp.net MVC 应用程序中,我在尝试使用 directoryEntry.Invoke 重置密码时遇到 Access denied 错误。

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

相关代码:

directoryEntry.Invoke("SetPassword", new object[] { model.Password });
                directoryEntry.Properties["LockOutTime"].Value = 0;
                directoryEntry.Close();

确切的错误是——

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 –

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

已经在网上查过了,但我不清楚可能是什么问题。我看到的最相关的是这个 - Setting ASP.Net Permissions - Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

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

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

【问题讨论】:

  • 你能解决这个问题吗?

标签: asp.net iis active-directory ldap directoryservices


【解决方案1】:

我们有类似的要求来更改或重置密码。我们使用以下代码sn-p。

        /// <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)){ 的上下文中运行代码userEntry.Invoke("SetPassword", newPassword);

我的意思是entry 代表持有域管理员用户和密码的对象。此管理员用户必须拥有完全权限才能在 AD 中进行更改。

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

【讨论】:

  • 因此,不是委派部分控制权,而是拥有一段具有域管理员帐户权限的代码以及该帐户在代码和/或配置文件中指定的用户名/密码?如果不能使用,委托有什么意义?
猜你喜欢
  • 1970-01-01
  • 2012-05-19
  • 2014-11-19
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
相关资源
最近更新 更多