Recently, I got to perform a task during a SharePoint 2010 web part development, where it was required to check for the DNS validity. I needed to see if a newly built virtual-server was hosted correctly and DNS settings updated accordingly to make the server available over the enterprise network.
To start with, we need to make use of the class [IPHostEntry] available to us in the System.Net namespace. This will provide us a container class for Internet host address information. So we need to add this to the Project References and include the using statement:
using System.Net;
We will also use the [Dns] class in the System.Net namespace which will provide the simple domain name resolution functionality. This class has the GetHostEntry() method that will resolve the host name for us to an IPHostEntry instance.
So let’s jump into the code. Assuming, we have a textbox “txtServerName” and a button on the form named “btnValidate”.
protected void btnValidate_Click(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
string[] strRetValue = { "0", "0", "0" };
strRetValue = ValidateDNS(txtServerName.Text.ToString());
if (strRetValue[0].ToString() == "0")
{
//Display the error message: "DNS Validation failed!
//Please verify the name of the server or check if the server
//has been provisioned correctly."
…….
//Your code when validation fails.
}
else
{
// Display the Success message: "DNS Validation SUCCEEDED."
…….
//Your code.
}
}
}
In the above code, we are simply trying to call the ValidateDNS() method. We pass the name of the server to be validated and grab the results of validation in the array of strings strRetvalue[].
This array contains two elements.
- 1st element would store either a “0” = InValid or “1” = Valid.
- 2nd element would store the actual host name if 1st element is “1” and NULL if it’s “0”.
- 3rd element would store any validation error messages generated.
So when we return from the ValidateDNS() call, we check to see the first element.
If it is equal to “0”, the validation had failed, else succeeded.
Now it’s time for the actual validation code.
Here’s what it looks like:
private string[] ValidateDNS(string p)
{
string[] retValue = { "0", "0", "0" };
try
{
IPHostEntry ipEntry; //The IP or Host Entry to lookup
char[] myArray = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ-".ToCharArray(); //Value of alpha characters
if (p.IndexOfAny(myArray) != -1) //If we see alpha characters,
//that means it’s a forward lookup
{
//Resolve a host name to IPHostEntry instance.
ipEntry = Dns.GetHostEntry(p.ToString());
//Count returns number of elements in sequence.
if (ipEntry.AddressList.Count() > 1)
{
retValue[0] = "1"; // Valid
retValue[1] = ipEntry.HostName.ToString();
}
else
retValue[0] = "0"; //InValid
}
//If no alpha characters, then it’s a reverse lookup
else
{
ipEntry = Dns.GetHostEntry(p.ToString()); //Resolve it
}
}
catch (System.Net.Sockets.SocketException se)
{
//The system had problems resolving the address passed.
retValue[2] = se.Message.ToString();
}
catch (System.FormatException fe)
{
//Some non-unicode characters were passed.
retValue[2] = fe.Message.ToString();
}
return retValue; //Finally return the array.
}
I have explained each line of code with comments inline, so it shouldn’t be that hard to understand here.
This was all that was needed to do the DNS validation in a C# web-part code.
No comments:
Post a Comment