Is it possible, pragmatically, to tell whether an ASP.NET web app is running in the Cassini web server in Visual Studio, or in IIS?
Background and reason for asking:
In quite a few of our apps, we bypass the normal login settings in a login page by checking the machine name, or by checking to see if the Client IP is localhost. (127.0.0.1 or :::1) so that we can test and have the app behave differently while debugging on our development PCs.
The Machine name approach is OK, but the wee need to change the code when we get a new Dev PC. I realize this isn’t too often, but it feels sloppy. Also, with several developers working on it, we have to list all of our machines in the “if” statement.
As for the “localhost” approach, I don’t like it. It seems to add another attack surface. I worry about someone getting onto one of the web servers and running it locally and seeing stuff.
So I wonder if there’s a way from code to detect that the website is running from Visual Studio with the Cassini web server.
When running under IIS the
SERVER_SOFTWAREproperty of theServerVariablescollection reports something likeMicrosoft-IIS/7.5.Unfortunately this appears to be blank when running under Cassini.
I don’t know if this helps you as it seems the opposite way round to the desired behaviour if you are concerned about security.
from comments:
Check the System.Diagnostics.Process.GetCurrentProcess().ProcessName For Cassini it’s “WebDev.WebServer40” and for IIS it’s “w3wp”
If this is running in IIS, the pName variable will be “w3wp. In Visual Studio on a .NET 4.0 app, it’s “WebDev.WebServer40”. (I haven’t checked but I assume it would be WebDev.WebServer20 for ASP.NET 2.0)