Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want to change the network card configuration as the IP address, subnet, gateway and DNSs. All this using ASP.NET 4.0 with C# deployed in Windows XP IIS 5.1. I'm using WMI for this purpose, but when I run the code not work.

Specifically I can show the IP, Subnet, Gateway and DNS of network. But I can't set the values.

My idea is configure the network card remotely (as a router).

What I have tried:

I tried to make some things similar to this example:
SwitchNetConfig - Laptop users, quickly switch network and proxy configuration in different places[^]

The code for the WMI helper class:

C#
<pre>using System;
using System.Collections;
using System.Management;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NetworkConfig
{
    public class WMI_helper
    {
        /// <summary>
        /// Obtiene las tarjetas de red
        /// </summary>
        /// <returns></returns>
        public static ArrayList GetNICNames()
        {
            ArrayList nicNames = new ArrayList();

            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();

            foreach(ManagementObject mo in moc)
            {
                if ((bool)mo["ipEnabled"])
                {
                    nicNames.Add(mo["Caption"]);
                }
            }
            return nicNames;
        }

        /// <summary>
        /// Obtiene la IP de una tarjeta de red específica
        /// </summary>
        /// <param name="nicName">Nombre de la tarjeta de red</param>
        /// <param name="ipAddresses">Direccion IP</param>
        /// <param name="subnets"></param>
        /// <param name="gateways"></param>
        /// <param name="dnses"></param>
        public static void GetIP(string nicName, out string[] ipAddresses, out string[] subnets, out string[] gateways, out string[] dnses)
        {
            ipAddresses = null;
            subnets = null;
            gateways = null;
            dnses = null;

            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();

            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["ipEnabled"])
                {
                    if (mo["Caption"].Equals(nicName))
                    {
                        ipAddresses = (string[]) mo["IPAddress"];
                        subnets = (string[]) mo["IPSubnet"];
                        gateways = (string[])mo["DefaultIPGateway"];
                        dnses = (string[]) mo["DNSServerSearchOrder"];

                        break;
                    }
                }
            }
        }

        /// <summary>
        /// Establece las configuraciones de red para una tarjeta de red especifica.
        /// </summary>
        /// <param name="nicName">Nombre de la tarheta de red</param>
        /// <param name="IPAddresses">Direccion IP</param>
        /// <param name="subnetMask">Subnet</param>
        /// <param name="gateway">Gateway</param>
        /// <param name="DnsSearchOrder">DNS</param>
        public static void SetIP(string nicName, string IPAddresses, string subnetMask, string gateway, string DnsSearchOrder)
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();

            foreach (ManagementObject mo in moc)
            {
                if((bool)mo["IPEnabled"])
                {
                    if (mo["Caption"].Equals(nicName))
                    {
                        ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic");
                        ManagementBaseObject newGate = mo.GetMethodParameters("SetGateways");
                        ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");

                        newGate["DefaultIPGateway"] = new string[] { gateway };
                        newGate["GatewayCostMetric"] = new int[] { 1 };

                        newIP["IPAddress"] = IPAddresses.Split(',');
                        newIP["SubnetMask"] = new string[] { subnetMask };

                        newDNS["DNSServerSearchOrder"] = DnsSearchOrder.Split(',');

                        ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null);
                        ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGate, null);
                        ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);

                        break;
                    }
                }
            }
        }

        public static void SetDHCP(string nicName)
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();

            foreach (ManagementObject mo in moc)
            {
                if (mo["Caption"].Equals(nicName))
                {
                    ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
                    newDNS["DNSServerSearchOrder"] = null;
                    ManagementBaseObject enableDHCP = mo.InvokeMethod("EnableDHCP", null, null);
                    ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
                }
            }
        }

    }
}
Posted
Updated 16-Mar-17 7:05am
v2
Comments
F-ES Sitecore 16-Mar-17 12:21pm    
Post the code you current have and explain what "doesn't work" means as that statement doesn't help anyone narrow down what the problem might be.
[no name] 16-Mar-17 12:29pm    
Your website administrator might not like it when you arbitrarily start changing his network settings on his server.
MarioCortess 16-Mar-17 12:43pm    
The website don't be public. Only will be accessed from local network with access for a single administrator. The context is that I have have a embedded computer inside of aluminium enclosure with two network card. The idea is configure easily without open the enclosure every time that I required it. I don't know if there other solution for this.
Dave Kreskowiak 16-Mar-17 12:45pm    
OK, so what O/S is this "embedded" computer running? Is it also running the web server?
Dave Kreskowiak 16-Mar-17 12:44pm    
ASP.NET app? Nothing like making it harder for yourself.

So, on which machine are you doing this configuration change? On your web server? On some other machine? Are they in a domain environment?

1 solution

You have to run your web site under a different account, one specifically setup to run the site AND have enough admin permissions to make changes to the network configuration.

The default ASP.NET account does not have permissions to make those changes.

Other than that, it doesn't matter what kind of app is using WMI to change the network config. It's exactly the same code for any application.
 
Share this answer
 
Comments
MarioCortess 23-Mar-17 13:03pm    
Thanks Dave. I've solutionaed the problem. It was all about permissions. I changed the asp.net account to administrator group.
Dave Kreskowiak 23-Mar-17 15:21pm    
<sarcasm>Yeah, that's not dangerous at all.

There's a reason why I said setup a user account that has the permissions to do it. Giving the default ASP.NET account admin permissions to the box is about as dangerous as giving the nuclear button to a band of chimpanzee's.

You give a web server account only the permissions it needs to do it's job. Anything more than that and you allow hostile code full admin permissions to your entire machine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900