使用System.DirectoryServices.AccountManagement查找组成员身份

时间:2012-01-25 16:25:09

标签: active-directory directoryservices account-management

我正在尝试使用.NET 4应用程序(VisualStudio 2010)中AccountManagement命名空间/程序集中的类型来针对Active Directory对用户进行身份验证。这是我的代码:

private Boolean ValidateUser(String domainName, String userName, String password)
{
    var ou = String.Format(CultureInfo.InvariantCulture,
                           "LDAP://{0}.mydomain.com/dc={0},dc=mydomain,dc=com",
                           domainName);

    var domain = String.Format(CultureInfo.InvariantCulture,
                               "{0}.mydomain.com",
                               domainName);

    using (var context = new PrincipalContext(ContextType.Domain,
                                              domain,
                                              ou))
    {
        if (context.ValidateCredentials(userName, password))
        {
            var userPrincipal = UserPrincipal.FindByIdentity(context,
                                                             IdentityType.SamAccountName,
                                                             userName);

            return userPrincipal.IsMemberOf(context, IdentityType.Name, "GroupName");
        }

        return false;
    }
}

代码运行良好,直到我调用FindByIdentity的语句。此调用会导致以下异常:

System.DirectoryServices.AccountManagement.PrincipalOperationException was caught
  Message=Unknown error (0x80005000)
  Source=System.DirectoryServices.AccountManagement
  ErrorCode=-2147463168
  StackTrace:
       at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit()
       at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
       at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
       at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
       at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
       at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue)
       at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue)
       at Dominos.Pulse.Server.Security.DirectoryServices.ActiveDirectoryAuthenticationProvider.ValidateUser(String domainName, String userName, String password)
  InnerException: System.Runtime.InteropServices.COMException
       Message=Unknown error (0x80005000)
       Source=System.DirectoryServices
       ErrorCode=-2147463168
       StackTrace:
            at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
            at System.DirectoryServices.DirectoryEntry.Bind()
            at System.DirectoryServices.DirectoryEntry.get_SchemaEntry()
            at System.DirectoryServices.AccountManagement.ADStoreCtx.IsContainer(DirectoryEntry de)
            at System.DirectoryServices.AccountManagement.ADStoreCtx..ctor(DirectoryEntry ctxBase, Boolean ownCtxBase, String username, String password, ContextOptions options)
            at System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(DirectoryEntry entry)
            at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit()
       InnerException: 

显然我的配置错误了。如果没有,也许我只是以错误的方式解决这个问题。

我的目标是简单地针对A / D对用户进行身份验证,然后确保他们是特定组(或组)的成员。我做错了什么?

1 个答案:

答案 0 :(得分:0)

你能尝试像这样用餐:

var ou = String.Format(CultureInfo.InvariantCulture,
                      "dc={0},dc=mydomain,dc=com",
                      domainName);

根上下文不需要验证凭据。