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

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:

/// <summary>
/// Created an entry in DNS Server on a Windows 2003 Server
/// </summary>
/// <param name=”Domain”>Domain name and TLD Extension</param>
/// <param name=”AssignToIpAddr”>IP Address the site belongs to in IIS</param>
/// <param name=”PrimaryServerIP”>IP Address of the DNS Server itself</param>
/// <param name=”AdminEmailName”>Email of the administrator for this domain</param>
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 + “<BR>”);
        HttpContext.Current.Response.Write(ex.StackTrace);
        HttpContext.Current.Response.Write(ex.ToString());
        HttpContext.Current.Response.End();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *