Login to active directory

521 views Asked by At

Hi I have an active directory that is installed on a machine that belong to different domain than the domain I am doing development on.

And there is no trust relationship between the two domains.

Let's say that domain called "test.com" .

there is an administrator called "test\administrator" with password "admin_password"

When I set up these setting with ActiveDirectoryMembershipProvider, I will be able to access, and login in to that Active directory and validate users. I am using these settings

<configuration>
  <connectionStrings>
        <add name="ADService" connectionString="LDAP://test.com"/>
  </connectionStrings>
  <system.web>
     <compilation debug="true" targetFramework="4.0" />

      <authentication mode="Forms"> </authentication>

      <membership defaultProvider="AspNetActiveDirectoryMembershipProvider">
        <providers>
          <add name="AspNetActiveDirectoryMembershipProvider" connectionStringName="ADService" connectionUsername="test\administrator" connectionPassword="admin_password"
               type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"
               connectionProtection="None" requiresUniqueEmail="false" enableSearchMethods="true" applicationName="/"/>
        </providers>
      </membership>
</configuration>

But when I try to write code from the same web app, or from another app , and try access the active directory from the code, I got error that login failed, unknown user name or password.

and this is my code

DirectoryEntry de = new DirectoryEntry("LDAP://test.com", "test\administrator", "admin_password", AuthenticationTypes.ServerBind);

            var ds = new DirectorySearcher(de) { Filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))", "[email protected]") };
            SearchResultCollection users = ds.FindAll();

I tried all types of AuthenticationTypes, and nothing worked Any idea please

1

There are 1 answers

0
Ghassan Karwchan On BEST ANSWER

I found out the problem it was very stupid the domain name should be capital , so the user name should be TEST\administrator

I am surprised that the membership provider was able to deal with this.