Using Connect-MsolService inside an ASP.net Web Application

I provide a number of useful tools to our 1st and 2nd line teams through a web application – mainly because it is flexible, easy to update and centralised.

We are launching Office 365, as a side-by-side service. Not all of our users are entitled to it (through various reasons) so checking whether or not a user has been assigned a license or service quickly is important.

I made a simple script that I could put behind a web form. It took the username as input, connected to the Azure directory and then returned a pretty output of the licenses and the service plans that the user was assigned. Testing locally in the PowerShell ISE worked great.

However, after putting it up in the web application it started faililng. Connect-MsolService would fail every time to establish a connection.

This is because the Connect-MsolService cmdlet uses the local user profile to cache information when it connects. If the profile is not available (which is the default setting when creating a web application) then the cmdlet fails to make a connection.

The solution is simple though. In the properties of the Application Pool that is hosting your application, change the setting of ‘Load User Profile’ to ‘true’.

To change this setting:

  1. Open the IIS Console, and select Application Pools from the pane on the left
  2. Select the application pool that is assigned to your web application (if you are unsure you can right click the pool and select View Applications)
  3. Select Advanced Settings
  4. Change the value of ‘Load User Profile’ to true and click Ok.
    IIS Application Pool Load User Profile setting
  5. Select Recycle on the Action pane to recycle the application pool.

Now your ASP.net web application should be able to use the Connect-MsolService cmdlet.

Be the first to like.


Leave a Reply

Your email address will not be published. Required fields are marked *