DNS setup Impact the AD authentication in C#
<p>Since I saw lot of posts about the exception which is shown below, And most of guys were stuck with it painfully.</p>

<pre><code>Exception Message: "The server is not operational"
Source: "System.DirectoryServices"
</code></pre>

<p>Recently It happened on me. But finally I figure it out .I thought I should share my experience here so that in future it could be helpful for someone else who has the same problem as me.  </p>

<p>The program I was working with is using AD authentication. And it works fine in my computer which belongs to the AD Domain. The code is below.</p>

<pre><code> public static bool IsAuthenticated(string srvr, string usr, string pwd)
        {
            bool authenticated = false;

            try
            {
                DirectoryEntry deRoot = new DirectoryEntry(srvr);

                DirectoryEntry entry = new DirectoryEntry(srvr, usr, pwd);
                object nativeObject = entry.NativeObject;//this will cause exception Until setting the right DNS address.
                authenticated = true;
            }
            catch (DirectoryServicesCOMException cex)
            {
                //not authenticated; reason why is in cex
                HttpContext.Current.Response.Write(cex.Message);
            }
            catch (Exception ex)
            {
                //not authenticated due to some other exception [this is optional]
                HttpContext.Current.Response.Write(ex.Message);
            }

            return authenticated;
        }
</code></pre>

<p>One day as our IT environment requirement changed, My computer IP is switched from 10.50.70.64 to 169.254.135.249. Because my net adapter IP and DNS always set to <code>Obtain address automatically</code>.In this case , I can log in my computer using the AD account successfully. But the program run with a big fat exception. So I doubted if there is something wrong with my Net Adapter settings. Finally I found If I set the right DNS address of our IT environment. The Exception was gone. I don't know why. So I also hope someone can explain more about it. Thanks.</p>
