Create ASP.NET virtual directories in IIS Express using windows credentials to another server (website project)

Moving from developing ASP.NET sites against the full IIS to IIS Express led me to migrating the many virtual directories that were defined for the site. There was an added dimension to the problem, in that the the virtual directories map to central development resources server to keep the many GB of images and tens of thousands of pdf files off the developer SSD drives, for space reasons (and sanity).

To my relief it turns out to be totally possible for website projects (as oppose to website application projects), but I didn’t on performing a quick internet search, find any reference to credentials.

The virtual directories can be set up as part of the site definition in the .vs folder within the applicationhost.config of the website solution. By editing that file, under the following node:

<site
  <application path= …
      <virtualDirectory path= …

Luckily rather than in notepad, I opened up this file in Visual Studio, and found intellisense gave me the answer I had been looking for, and it was a good one!

virtualdirectory

You can see there are userName and password attributes available.

So add a virtualDirectory node for each virtual directory, following this pattern, one after another, after the application node.

<virtualDirectory path="/imageitems" physicalPath="\\resServer\Resources\ProdRes"  
userName="devaccess@mydomain.net" password="ourpassword123" />

We use a read only, low rights user for this, so I’m not too bothered about the password getting into the config file.

Convert ASP.NET “Website Project” from IIS to IIS Express

I had this problem to solve and thought I’d document my solution here, as I didn’t find anything appropriate documented when I tried searching the search engines.

Website Project vs Website Application Project

There are two broad ways ASP.NET websites can be setup as applications.

  • Web site projects
  • Web site application projects

The quickest way to tell the difference is that an application project uses a visual studio project file to define the files that are in the project, whereas for a web site project, any file under the folder is classed as part of the project.

For more information look at: Web Application Projects versus Web Site Projects in Visual Studio (msdn)

Web site projects are opened by going to File->Open Website, rather than File->Open Project, obviously there are many ways to open projects.

I labour this point as on the forums it is almost the norm for questions about website projects get people answering as if it were a website application project, understandable as that is the form of project most developers will experience.

Finally I decided to tackle the problem of being required to have local administrator rights to develop with IIS hosted sites. Running with local admin rights in this day and age is becoming more and more risky and something that is getting less and less necessary so long as you have an alternative elevated user to use when the need arises to perform operations that require it (like installing software). 

These days developing sites against IIS Express is the norm, back in the day we outgrew Cassini, a small developer webserver that used to host sites, as we had url rewriting, host headers, custom url rewrite modules, virtual folders mapped with custom credentials etc. It turns out that now in 2017 all this is possible using IIS Express. How do you move the site though?

These are the steps I performed that worked for me:

Remove the IIS website

In IIS manager I removed the website, so IIS is no longer managing that folder.

Edit the solution file

I edited the solution file to remove the website project definition node from it.

Add the website back in

I then opened the solution and used the add existing website option to add the website folder from source control back into the solution.

Define virtual directories

I then edited the applicationhost.config file that resides in the solution .vs folder, defining the virtual directories that were previously defined in IIS. I did this under the <virtualDirectory path= section for the site.

Added https binding

I added a binding to the <bindings> section for https,
<binding protocol="https" bindingInformation="*:44340:localhost" />

Custom rewriting 

The custom URL rewriter and rewrite rules seemed to come over by themselves so no action was needed.

Custom Host name

I followed the instructions on USING A CUSTOM HOSTNAME WITH IIS EXPRESS AND VISUAL STUDIO 2015 (VS2015)

to get the custom hostname so the site responds to custom host headers correctly.

<binding protocol=”http” bindingInformation=”*:80:mywebapp.127.0.0.1.xip.io” />

Success

I was then amazed to see the site come up first time on rebuilding it and launching it.

Visual Studio menu font size stuck after presenter mode

Presenter mode in visual studio (accessed via the quick launch at top right), allows VS to switch into a larger font layout, ideal for LCD projector presentations.

Presenter Mode Visual Studio

Whilst I had this mode on I had a crash in visual studio and had messed with other settings One of these meant that when I switched presenter mode off, it was no longer taking the font size of the Visual Studio menus back down to normal. Although for a couple of days I lived with the large fonts, I finally looked at it today. Get yourself to the following Visual Studio

Tools>>Options>>Environment>>Fonts and Colors *US spelling

or type fonts into the quick launch, quick launch is really helpful for these kind of things…

Quick Launch - To Find Fonts and Colors

Now in the options window use the drop down to to select Environment, followed by clicking the “Use Defaults” button. When the overall window is “OKed”, then Visual Studio will return to normal, this same procedure can also be used to correct any of the other options in the drop down box.

Font Settings set Environment & click Use Defaults

After doing this, presenter mode on/off works again as expected.

More about presenter mode

.NET Power Tip 6: Presenting in Visual Studio (Presentation Mode & ToolBox Snippets)

6 Quick Tips for Presenting Code in Visual Studio