I ran into an interesting bug at work this week when discovered the .NET Framework SDK QuickStarts failed to setup up properly on German versions of Windows 2003 Server. The application that we use to setup up the sample databases with SQL grants the network service account access so that ASP.NET application will work properly. It turns out that on German versions of Windows 2003 Server the Network Service account is called NT-AUTORITÄT\NETZWERKDIENST. We had our code hardcoded to use NT Authority\NETWORK SERVICE. After some investigation on the development team it turns out that .NET provides an ENUM of well known OS accounts that you can access via managed code.

System.Security.Principal.WellKnownSidType provides access to accounts like Network Service that can be renamed on localized operating systems.

Way that you want to programmatically specify the NETWORK SERVICE account is to use some code like this:

 

using System;
using System.Security.Principal;
namespace ConsoleApplication2
{

   class Program
   {

      static void Main(string[] args)
      {

         SecurityIdentifier si = new SecurityIdentifier(WellKnownSidType.NetworkServiceSid, null);
         Console.WriteLine(si.Translate(typeof(NTAccount)).ToString());
         Console.Read();

      }

   }

}