ASP.Net Core Razor Page and IIS Administration API

I have been looking around for an elegant way of start, stopping and restarting (at least) IIS Websites and Application Pools from script automated. All the way of doing so required administrator permission. That did not work for me since I was interested in automating that work. For some months ago, I just found IIS Administration API. This was execltly what I was was looking for.

api-620x440

I found this API more and less at the same time NetCoreApp 2.0 was released, so I thought I could test the 2.0 release with playing around with the IIS Administration API. The site I wanted is quite simple, listing IIS Websites and for each site have one (start) button if the site is stopped and 2 (restart / stopp) if the site is started.

Picture of the first goal

Installation if the API is made really simple by clicking https://manage.iis.net/get the button on that site. Then follow the instructions. When the installation is complete, it is ready to visit https://localhost:55539 and explore the API and create access keys.

The IIS Administration API is a clean REST service that do execly what is expected when using it. The one thing I got a little bit stuck was when I published the site to IIS. I just got 401 Unathorized and could not understand why. You may need to set the permission to who can acces the API in the configuration file as well and the user running your IIS site need to be the user you give access to. The appsettings.json file was located at this location C:\Program Files\IIS Administration\2.1.0\Microsoft.IIS.Administration\config.

"security": {
    "require_windows_authentication": true,
    "users": {
      "administrators": [
        "DOMAIN\\username"
      ],
      "owners": [
        "DOMAIN\\username"
      ]
    },
    "access_policy": {
      "api": {
        "users": "administrators",
        "access_key": true
      },
      "api_keys": {
        "users": "administrators",
        "access_key": false
      },
      "system": {
        "users": "owners",
        "access_key": true
      }
    }
  }

When I got that permission correct, I did not have any more issues with the API and it still works fine.

Teis Lindemark

Software developer, beer brewer and AGENT backer

Bergen, Norway https://teilin.net

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.