Search code examples
asp.netpermissionspinvoke

best way to get a volume serial number in an asp.net application?


I work on an ASP.Net application which is a web version of an already existing winforms desktop application. The desktop application reads a license file defining enabled features to adapt its behavior. To lock the application to a specific machine, one of the setting of the license file is the serial number of the first system volume. At startup the application checks that the serial number in the license file matches the volume serial number. Getting the volume serial number is done by PInvoking kernel32.dll's GetVolumeInformation function.
However in the asp.net version a standard application pool using a local service or local network identity does not have permission to PInvoke, resulting in the impossibility to check the license file is valid. How can I check the license at the application startup?

I can think of the following alternatives:

  • replacing the PInvoke by a method which does not require special permissions to get the volume serial number (is it the case with WMI?)
  • putting all the license checking code in a separate assembly and install it to the GAC to have it executed elevated
  • creating an application pool with administrator identity just for my application during the installation process

The first solution would be the best but I don't know if it is possible.
Have I other possibilities? What are the pros and cons of each method? Which one is the best?


Solution

  • I suggest using WMI. The Win32_Volume class has the serial number of the volume. You can use something like this:

    using System.Management;
    SelectQuery query = new SelectQuery("Win32_Volume");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    foreach (ManagementObject obj in searcher.Get())
    {
        Console.WriteLine("{0} {1}", obj["DriveLetter"], obj["SerialNumber"]);
    }