【问题标题】:Exception has been thrown by the target of an invocation. thrown at UserPrincipal调用的目标已引发异常。扔在 UserPrincipal
【发布时间】:2014-07-22 14:24:32
【问题描述】:

我正在尝试在 AD 中创建一个新用户。我正在使用以下代码:

        using (var context = new PrincipalContext(ContextType.Domain, this.DomainName, userWithWMIAccessRights, userWithWMIAccessrightsPassword))
        {
            UserPrincipal user = new UserPrincipal(context);
            user.DisplayName = Newusername;
            user.Name = Newusername;
            user.Enabled = true;
            user.SetPassword(passwordOfThenewUser);
            user.Save(); //<-- throws exception InnerException = {"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"}
            return user;
        }

我这样设置了 userWithWMIAccessrightsPassword:

  • userWithWMIAccessrightsPassword 是 Enterprise Admins、Domain Admins 和 Domain Users 的成员
  • 从命令提示符运行 dcomcnfg。
  • 从服务列表中选择了 Windows Management Instrumentation。
  • 通过从“安全”选项卡中添加 userWithWMIAccessrightsPassword 帐户更新了自定义权限,并授予他完全权限。

Web 应用程序与 AD 运行在同一台机器上。当我使用 Visual Studio 调试 web 应用程序时,用户和密码设置没有问题。

当 Web 应用程序部署在 AD 机器上时,创建用户的函数会在设置密码的代码行中抛出 {"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"}。用户已创建但未启用且没有密码。

当 PrincipalContext 使用具有完全权限的 AD 用户时,为什么我会拒绝访问?

【问题讨论】:

    标签: asp.net-mvc active-directory adfs


    【解决方案1】:

    好的,看来 PrincipalContext 是 != 冒充的。

    我可以通过以下方式解决问题:

    1.) 设置 PrincipalContext 的容器并调用 ValidateCredentials(this.UserName, this.Password,ContextOptions.Negotiate)。

    2.)

    using (new Impersonator(this.UserName, this.DomainName, this.Password, LogonType.LOGON32_LOGON_SERVICE, LogonProvider.LOGON32_PROVIDER_WINNT40))
            {
                using (var context = CreatePrincipalContext())
                {
                    UserPrincipal user = new UserPrincipal(context);
                    user.DisplayName = username;
                    user.Name = username;
                    user.Enabled = true;
                    user.SetPassword(password);
                    user.Save();
                    return user;
                }
            }
    

    但我仍然对具有 Principalcontext 的模仿者类的需求感到困惑。我认为 PrincipalContext 取代了模拟类。

    【讨论】:

      猜你喜欢
      • 2019-01-22
      • 2018-11-08
      • 2018-02-14
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      相关资源
      最近更新 更多