0

Due to permissions, I'm having trouble connecting to a remote server (Windows Server 2016) using c# when code is deployed on IIS. When I run it on visual studio it runs ok but on IIS I'm getting an unauthorized exception.
Stacktrace : System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at System.Management.ManagementScope.InitializeGuts(Object o) at System.Management.ManagementScope.Initialize() at System.Management.ManagementObjectSearcher.Initialize() at System.Management.ManagementObjectSearcher.Get()

Code snippet that I used is:

    ConnectionOptions connectoptions = new ConnectionOptions();
    //connectoptions.Impersonation = ImpersonationLevel.Impersonate;
    connectoptions.Username = username;
    connectoptions.Password = password;
    
    //IP Address of the remote machine
    ManagementScope scope = new ManagementScope(@"\\" + ip + @"\root\cimv2");
    scope.Options = connectoptions;
    //WMI query to be executed on the remote machine
    SelectQuery query = new SelectQuery("select * from Win32_Service where name = '" + serviceName + "'");

    using (ManagementObjectSearcher searcher = new
                ManagementObjectSearcher(scope, query))
    {
        ManagementObjectCollection collection = searcher.Get();
        Console.WriteLine("Logged in");
        foreach (ManagementObject service in collection)
        {
            Console.WriteLine(service);
        }
    }
Redi
  • 1
  • 1
  • Are you an Admin? Are you running from inside Visual Studio? visual Studio doesn't automatically give Admin rights. You must right click shortcut of VS and select Run As Admin. the c# executable will automatically have Admin rights. – jdweng Aug 22 '22 at 13:03
  • 1
    you need to provide credentials to connect to the remote server (assuming iis runs as network service) , you need to use/create a user on your remote server and impersonate as said user – Bacon Aug 22 '22 at 13:06
  • @jdweng when i try from visual studio it works even when I haven't started it as an admin but only when deployed it to iis I'm facing permission issues. – Redi Aug 22 '22 at 13:17
  • @Bacon i provided the admin credentials of remote server that's why its working on visual studio. Not sure why it isn't when deplyed. – Redi Aug 22 '22 at 13:18
  • https://halfblood.pro/web-application-differences-in-visual-studio-and-iis-60fec7e311b3 Beginners can easily miss the huge differences out there. – Lex Li Aug 22 '22 at 15:07
  • You need admin on both local and remote machine (and same group account) if you are accessing remotely. I would try directly on IIS and see if it works. – jdweng Aug 22 '22 at 16:01
  • I reckon what is actually happening is that the username and password are not being used at all (for whatever reason). Instead it is just using your already logged in credentials. And your current user in VS happens to also be an admin on the remote machine. Meanwhile IIS is using a local account (local to the machine, rather than domain-wide) and therefore has no rights on the remote machine. You may want to take a read of https://stackoverflow.com/questions/14934006/iis-iusrs-and-iusr-permissions-in-iis8 – Charlieface Aug 22 '22 at 23:49
  • Have you tried setting the application pool identity to `NetworkService`? – JennyDai Aug 23 '22 at 06:24

0 Answers0