error 26 with Active Directory RRS feed

  • Question

  • Hi everybody


    I am using iis 6.0 and visual studio 2005.
    I have created a simple login.aspx page to authenticate users in Active Directory (AD) by using LDAP. when I click on F5 and debug the project everything goes well without any problem. But when I right click on the project and choose publish and then browse to the web site it generates a long error message like:


    An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)


    the problem is I have nothing to do with SQL server at all in my code.

    here is my web.config file


    <add name="ADConnectionString" connectionString="LDAP://,DC=fine,DC=com"/>



    <siteMap defaultProvider="default">



    <add name="default" type="System.Web.XmlSiteMapProvider" siteMapFile="web.sitemap" securityTrimmingEnabled="true"/>



    <roleManager enabled="true" />

    <compilation debug="true" strict="false" explicit="true">


    <add assembly="System.DirectoryServices, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>




    <add namespace="System"/>

    <add namespace="System.Collections"/>

    <add namespace="System.Collections.Specialized"/>

    <add namespace="System.Configuration"/>

    <add namespace="System.Text"/>

    <add namespace="System.Text.RegularExpressions"/>

    <add namespace="System.Web"/>

    <add namespace="System.Web.Caching"/>

    <add namespace="System.Web.SessionState"/>

    <add namespace="System.Web.Security"/>

    <add namespace="System.Web.Profile"/>

    <add namespace="System.Web.UI"/>

    <add namespace="System.Web.UI.WebControls"/>

    <add namespace="System.Web.UI.WebControls.WebParts"/>

    <add namespace="System.Web.UI.HtmlControls"/>



    <authentication mode="Forms">

    <forms name="HIQForms" loginUrl="HiQLogin.aspx" timeout="10"/>



    <deny users="?"/>

    <allow users="*"/>


    <identity impersonate="true" />



    and here is my login_click method:


    protected void btnLogin_Click(object sender, EventArgs e)


    string userName = txtUserName.Text;

    string password = txtPassword.Text;

    string domain = "hiq";

    string LDAPPATH = ConfigurationManager.ConnectionStrings["ADConnectionString"].ConnectionString;

    string domainAndUsername = domain + @"\" + userName;

    DirectoryEntry entry = new DirectoryEntry(LDAPPATH, domainAndUsername, password);



    Object obj = entry.NativeObject;

    DirectorySearcher search = new DirectorySearcher(entry);

    search.Filter = "(SAMAccountName=" + userName + ")";


    SearchResult result = search.FindOne();

    if (null == result)






    // Update the new path to the user in the directory

    LDAPPATH = result.Path;

    ResultPropertyCollection myResultPropColl = result.Properties;

    //string name = myResultPropColl["sn"][0].ToString();

    FormsAuthentication.RedirectFromLoginPage(userName, true);



    catch (Exception ex)


    lblResults.Visible = true;

    lblResults.Text = "Unsuccessful login. Please re-enter your information and try again.";

    if ((Membership.GetUser(userName) != null) && (Membership.GetUser(userName).IsLockedOut == true))

    lblResults.Text += " Your account has been locked out.";



    Wednesday, March 26, 2008 1:59 PM



    Thank you a lot,


    I have solved my problem by adding following to my web.config file.

       <clear />
       <add name="ADConnectionString" connectionString="LDAP://,DC=fine,DC=com"/>

    <roleManager defaultProvider="AspNetWindowsTokenRoleProvider">
          <remove name="AspNetSqlRoleProvider" />


    but I have another problem now, when I browse to http://localhost it works like a charm but when I browse to http://MachineName it redirect me to the login page agian.
    Note: it seems that it authenticate me because when I insert wrong password or username it generates "Logon failure: unknown user name or bad password"

    thank you again

    Thursday, March 27, 2008 8:04 AM

All replies