Archive

Archive for the ‘DNS’ Category

Creating a DNS Entry in Windows 20003 Std Edition with c# .NET

June 25th, 2017 Michael Bell 2 comments

One of the websites I did in the past required me to be able to register a domain, create the site in IIS, add the DNS entries, and add email entries into HMail server. Here is the DNS server half of it. Of course you have to have permissions, but this is the method I used to get from A to B entering a new domain name into the DNS Server in Windows 2003 Standard Edition. You can tell by my exception block that this piece of code was executed from an ASP.NET Web Application:

///


/// Created an entry in DNS Server on a Windows 2003 Server
///

/// Domain name and TLD Extension /// IP Address the site belongs to in IIS /// IP Address of the DNS Server itself /// Email of the administrator for this domain public void new_Zone(string Domain, string AssignToIpAddr, string PrimaryServerIP, string AdminEmailName)
{
Domain = Domain.ToLower();
try
{
string[] ipArray;
ipArray = new string[1] { AssignToIpAddr };

//Create Foward Lookup Zone.
string sServerPath = “\\\\WINDOWS\\ROOT\\MicrosoftDNS”;
ManagementScope oScope = new ManagementScope(sServerPath);
oScope.Connect();
ManagementPath Path = new ManagementPath(”MicrosoftDNS_Zone”);
ManagementClass DnsZoneClass = new ManagementClass(oScope, Path, null);
ManagementBaseObject InputParams = DnsZoneClass.GetMethodParameters(”CreateZone”);

InputParams["ZoneName"] = Domain;
InputParams["ZoneType"] = 0;
InputParams["AdminEmailName"] = AdminEmailName;
InputParams["IpAddr"] = ipArray;
ManagementBaseObject OutParams = DnsZoneClass.InvokeMethod(”CreateZone”, InputParams, null);

#region A Records
ManagementPath newPath = new ManagementPath(”MicrosoftDNS_ATYPE”);
ManagementClass ARecordClass = new ManagementClass(oScope, newPath, null);
ManagementBaseObject ARecord = ARecordClass.GetMethodParameters(”CreateInstanceFromPropertyData”);ARecord["DnsServerName"] = “WINDOWS”;

ARecord["ContainerName"] = Domain;
ARecord["OwnerName"] = Domain;
ARecord["IPAddress"] = ipArray[0];

OutParams = ARecordClass.InvokeMethod(”CreateInstanceFromPropertyData”, ARecord, null);

ManagementClass dnsRRClass = new ManagementClass(@”\\” + System.Environment.MachineName + @”\root\MicrosoftDNS”, “MicrosoftDNS_ResourceRecord”, null);
ManagementBaseObject inParams = dnsRRClass.GetMethodParameters(”CreateInstanceFromTextRepresentation”);

inParams["DnsServerName"] = “.”;
inParams["ContainerName"] = Domain;
inParams["TextRepresentation"] = “www.” + Domain + ” IN A ” + ipArray[0];
OutParams = dnsRRClass.InvokeMethod(”CreateInstanceFromTextRepresentation”, inParams, null);

dnsRRClass = new ManagementClass(@”\\” + System.Environment.MachineName + @”\root\MicrosoftDNS”, “MicrosoftDNS_ResourceRecord”, null);
inParams = dnsRRClass.GetMethodParameters(”CreateInstanceFromTextRepresentation”);

inParams["DnsServerName"] = “.”;
inParams["ContainerName"] = Domain;
inParams["TextRepresentation"] = “mail.” + Domain + ” IN A ” + ipArray[0];
OutParams = dnsRRClass.InvokeMethod(”CreateInstanceFromTextRepresentation”, inParams, null);

#endregion
}
catch (Exception ex)
{
//throw;
HttpContext.Current.Response.Write(ex.Message + “
”);
HttpContext.Current.Response.Write(ex.StackTrace);
HttpContext.Current.Response.Write(ex.ToString());
HttpContext.Current.Response.End();
}
}

Categories: ASP.NET, DNS, IIS, c# Tags: