简体   繁体   中英

Determine if ASP.Net Core webapp is running on localhost

At my company developer machines are behind a proxy. I have a scenario where I need to provide my credentials for it and I am accomplishing it via the HttpClient.DefaultProxy. However, on Staging and Production servers there is no proxy. What I would like to do is determine if the application is running against local host and only set the default proxy in that scenario. In the past I've looked at the HttpRequest to determine that information. For this I need to apply the logic in the Startup file.

So far the only solution I've come up with that is somewhat clean is checking for an attached debugger. That isn't ideal though since we also run these applications locally without the debugger attached. Another option would be to use a middleware then check the url for "localhost" but that seems excessive.

Any ideas would be appreciated!

public void ConfigureServices(IServiceCollection services)
{
   if (/*Some clean way to determine IsLocal*/)
   {
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
   }
}

To clarify to prevent question removal - I would like to determine if localhost before an httprequest exists.


Update I've chosen to go with using lauchSetting.json which also isn't perfect but fits the best for my case. I'll go a head and outline all the options and their pros/cons for any future dev in the same position.

Option 1: Add Local Environment
Problems: If you have Environment specific configs already you'll spend time copy pasting configuration if you want to target "Staging" from your "Local" config file.
Code

public void ConfigureServices(IServiceCollection services)
{
   if (HostingEnvironment.IsEnvironment("Local"))
   {
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
   }
}


Option 2: Create a Middleware to check for localhost in request url
Problems: Depending on how its implemented it might be run too often. It also doesn't work in the event you're local version isn't using localhost in the url.

Option 3: Use launchSettings.json to house an environmentVariable
Problems: Depending on your IDE this may not work. It assumes you commit your launchSettings.json file.
Code

{
   "profiles": {
      "IIS Express": {
         "commandName": "IISExpress",
         "launchBrowser": true,
         "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development",
            "IsLocal": "True"
         }
      }
   }
}

public void ConfigureServices(IServiceCollection services)
{
   if (Environment.GetEnvironmentVariable("IsLocal") == "True")
   {
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
   }
}

Option 3.5: Use actual environment variable
Problems: Requires additional developer setup beyond checking out code and running

Option 4: Use Machine Names to determine if developer or server
Problems: Assumes all developer machines are using some kind of standard.
Code

public void ConfigureServices(IServiceCollection services)
{
   if (Environment.MachineName.StartsWith("DEV", StringComparison.OrdinalIgnoreCase))
   {
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
   }
}

Option 5: Check for an attached debugger
Problems: Debugger isn't always attached when you're running locally
Code

public void ConfigureServices(IServiceCollection services)
{
   if (Debugger.IsAttached)
   {
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
   }
}

Option 6: Use compile time debug check
Problems: Might not want to compile in debug in every scenario
Code

#if(DEBUG)
       //Need to provide proxy credentials for local development only
       var proxyCredentials = new NetworkCredential(Configuration["ProxyCredentials:Username"], Configuration["ProxyCredentials:Password"]);
       HttpClient.DefaultProxy = new WebProxy("http://proxy.company.com:80", false, null, proxyCredentials);
#endif

I would use a variant of your Option 4 with a small one-line whitelisting

if("|PC1|PC2|PC3|".Contains("|" + Environment.MachineName + "|"))
{
    // proxy code here
}

... and then be done with it.

I think that would be a good pragmatic solution.

Remember: perfect is the enemy of good.

I don't know the context but in case maybe you are the primary developer working on this corner of the codebase this would be perfectly good solution and you just inform your colleague if/when he needs to debug, that he needs to add his machine name to the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM