Remote access through c# program using Microsoft.Web.Administration

Discussion in 'Windows / IIS' started by Tonyg, Jun 24, 2018.

Tags:
  1. Hello,

    Has anyone used a c# program with Microsoft.Web.Administration to access a remote server?

    I am trying to use code similar the c# code found in this article: https://docs.microsoft.com/en-us/iis/configuration/system.webserver/security/ipsecurity/add in the Sample Code section.

    The problem I am having is on this line of code: ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", "Default Web Site");

    I can't figure out what to put in place of "Default Web Site".

    Thanks,
    Tony
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Try your domain name.
     
  3. RayH,

    Thanks for your help.

    I did try that and got the same error. I'm sorry I forgot to include the error being received in my opening post.

    The error is:
    "Filename: \\?\C:\WINDOWS\system32\inetsrv\config\applicationHost.config
    Error: Unrecognized configuration path 'MACHINE/WEBROOT/APPHOST/www.lakesideos.com' "

    I also tried using the Alternative URL(without the "http://") in the Domain/URL Information of my Account Information.

    Thanks,
    Tony
     
  4. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Hi Tony,

    I just realized the code won't work because you're trying to write to the applicationhost.config file which you do not have access to. The ipSecurity section has been delegated so you can make the changes to your web.config file. You'll probably have to use GetWebConfiguration() instead.
     
  5. Ray,

    Thanks for your help.

    The problem is there is no web.config file.

    If I can use the IIS Manager locally, go to Security\IP Address and Domain Restrictions\Add Deny Restriction Rule and add a Deny Restriction Rule, I should be able to do the same thing programmatically.

    That's what I want to do eventually as that article shows in the link that I posted.

    Thanks,
    Tony
     
  6. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Then you need to create the web.config file. All IIS Manager does is write the configuration changes to the web.config file anyways.
     
  7. Ray,

    I want to apologize for saying that there is no web.config before I investigated to see that there really is a web.config in the root of my website. It is populated with the Deny Restriction Rules that I entered manually through IIS Manager.

    In trying to use your suggestion of "use GetWebConfiguration()", I can't figure out how to make this code:
    Code:
                   using (ServerManager serverManager = new ServerManager())
                   {
                        Configuration config = serverManager.GetApplicationHostConfiguration();
                        ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", @"www.lakesideos.com");
                        ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();
    
                        ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
                        addElement["ipAddress"] = @"192.168.100.1";
                        addElement["allowed"] = false;
                        ipSecurityCollection.Add(addElement);
    
                        serverManager.CommitChanges();
                   }
    
    Into this code:

    Code:
                   using (ServerManager serverManager = new ServerManager())
                   {
    
                        Configuration config = serverManager.GetApplicationHostConfiguration(); 
                        Configuration ipSecuritySection = serverManager.GetWebConfiguration(@"www.lakesideos.com", "system.webServer/httpProtocol/security/ipSecurity");                            
                        ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();
    
                        ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
                        addElement["ipAddress"] = @"192.168.100.1";
                        addElement["allowed"] = false;
                        ipSecurityCollection.Add(addElement);
    
                        serverManager.CommitChanges();
                   }
    
    It does not find the GetCollection in the line: ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();

    Thanks,
    Tony
     
  8. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Hi Tony,

    I think you changed the wrong line. This line:

    Code:
    Configuration config = serverManager.GetApplicationHostConfiguration();
    
    Should read:

    Code:
    Configuration config = serverManager.GetWebConfiguration();
    
    I didn't fully test it, but I think your other lines are correct. More info on GetWebConfiguration().
     
  9. Ray,

    If I try this:
    Code:
                        Configuration config = serverManager.GetWebConfiguration();
    
    It says that there is no overload for method that takes 0 arguments.

    If I try this :
    Code:
          Configuration ipSecuritySection = serverManager.GetWebConfiguration(@"www.lakesideos.com", "system.webServer/httpProtocol/security/ipSecurity");
                        ConfigurationSection section = ipSecuritySection.GetSection("system.webServer/httpProtocol/security/ipSecurity");
                        ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();
    
    It says 'Configuration' does not contain a definition for 'GetCollection'.

    Thanks,
    Tony
     
  10. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    According to the documentation, it requires a string which is the Web site name. So probably:

    Code:
    Configuration config = serverManager.GetWebConfiguration(@"www.lakesideos.com");
    
     
  11. Ray,

    Thanks for your help.

    I do have a version of that in my second set of coding in my previous post.

    Thanks,
    Tony
     
  12. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Not sure why you added "httpProtocol"? It should be "system.webServer/security/ipSecurity". Also, you might want to remove the "www" from "www.lakesideos.com". The site name should just be "lakesideos.com"
     
  13. Ray,

    I don't know why I had "httpProtocol" in that line. I did remove it. I also removed the "www".

    I tried the code this way:
    Code:
                        Configuration ipSecuritySection = serverManager.GetWebConfiguration(@"lakesideos.com", "system.webServer/security/ipSecurity");
                        ConfigurationSection section = ipSecuritySection.GetSection("system.webServer/security/ipSecurity");
                        ConfigurationElementCollection ipSecurityCollection = section.GetCollection();
    
    When I run it, it gives this error:
    On this line:
    Code:
    ConfigurationSection section = ipSecuritySection.GetSection("system.webServer/security/ipSecurity");
    Thanks,
    Tony
     
  14. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Not sure why your code is still looking in the applicationHost.config file. You don't have access to that. After some research, I'm not sure the code will work because it seems like it requires Administrator access. You might want to try to modify the web.config file another way.
     
    Last edited: Jul 12, 2018

Share This Page