C#Active Directory FindByIdentity失败

时间:2017-03-02 21:56:42

标签: c# iis active-directory

好的,当用户输入用户通行证时,我的Active Directory查找工作正常。现在我正在尝试调整该代码以允许单点登录。我有什么:

  • IIS应用程序池设置为网络用户
  • 匿名身份验证在IIS中关闭
  • 用and和web配置设置     

代码:

WindowsIdentity userIdentity = WindowsIdentity.GetCurrent();
using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "mydomain.com"))
{
this.User = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, userIdentity.Name);

FindByIdentity因其中一个错误而失败,具体取决于不同的上下文更改:

未知错误(0x8000500c)堆栈:在System.DirectoryServices.PropertyValueCollection.PopulateList()

未知错误(0x80005000)堆栈:at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit()

我尝试过的事情包括,以“cn = Users,dc = mydomain,dc = com”和“dc = mydomain,dc = com”的形式将容器名称添加到上下文查找中以及是否包括包括ContextOptions.Negotiate与容器集或容器传递为null。

不知道下一步该去哪儿。我需要用户的所有组名,我尝试使用UserPrincipal.Current.GetGroups或其他任何组件获取组名,但它失败并出现与上述相同的PopulateList()错误。

我错过了一些简单的东西吗?如果我在上下文创建中提供用户/传递它工作正常,所以我不确定是否必须创建AD用户才能查询AD,但如果有的话,这不是一个很好的选择。

我已阅读有关此内容的所有帖子,似乎无法通过它。

1 个答案:

答案 0 :(得分:0)

错误0x8000500c表示Active Directory Datatype Cannot be Converted to/from a Native DS Datatype

导致此问题的原因是WindowsIdentity.Name的格式为DOMAIN\USERNAME且不是有效的SamAccountName

好像您正试图获取当前用户的UserPrincipal,如果是,请使用UserPrincipal.Current

相关问题