【发布时间】:2019-08-30 12:43:43
【问题描述】:
我必须为我的 asp.net 项目创建一个新的开发环境。它需要一个 AD 服务器,所以我在与我的 IIS 服务器和 IDE 相同的主机上创建它。
我把它转移过来并设置了所有新的环境变量,但我遇到了一个有趣的问题。
当要求它创建一个新的 AD 用户帐户时,我收到错误:
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)
at System.DirectoryServices.AccountManagement.SDSUtils.SetPassword(DirectoryEntry de, String newPassword)
at System.DirectoryServices.AccountManagement.ADStoreCtx.SetPassword(AuthenticablePrincipal p, String newPassword)
at System.DirectoryServices.AccountManagement.SDSUtils.InsertPrincipal(Principal p, StoreCtx storeCtx, GroupMembershipUpdater updateGroupMembership, NetCred credentials, AuthenticationTypes authTypes, Boolean needToSetPassword)
at System.DirectoryServices.AccountManagement.ADStoreCtx.Insert(Principal p)
at System.DirectoryServices.AccountManagement.Principal.Save()
方法(一直有效)创建用户,但它在 AD 中显示为禁用:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
Environment.GetEnvironmentVariable("DOMAIN"),
Environment.GetEnvironmentVariable("USER_OU"),
Environment.GetEnvironmentVariable("SERVICE_USERNAME"),
Environment.GetEnvironmentVariable("SERVICE_PASSWORD"));
UserPrincipalEx usr = new UserPrincipalEx(ctx);
usr.Name = Account.FirstName + " " + ticket.Account.LastName;
usr.SamAccountName = Account.Username;
usr.GivenName = Account.FirstName;
usr.Surname = Account.LastName;
usr.DisplayName = Account.FirstName + " " + ticket.Account.LastName;
usr.UserPrincipalName = Account.Username + "@" + Environment.GetEnvironmentVariable("DOMAIN");
usr.Company = Account.Company;
usr.Department = Account.Department;
usr.Description = Account.Description;
usr.SetPassword(temppwd);
usr.ExpirePasswordNow();
usr.Enabled = enabled;
try
{
usr.Save();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
我在生产中使用相同的代码,它工作得很好。我不明白为什么它在我的新开发环境中会出现这样的行为 - 或者什么权限允许它创建帐户,但不能启用它。
【问题讨论】:
-
您的异常详情不完整。您能否更新您的问题以包含实际的异常消息?另外,
usr是什么? -
抱歉,更新了消息和代码
-
那么您确实在 AD 中看到了新用户?
-
是的,它出现在 AD 中,但是它被禁用了
-
尝试在您的
SetPassword行之前调用usr.Save()。创建帐户后,我必须分两步完成(创建帐户,然后设置密码并启用它)
标签: c# asp.net-core active-directory