2

I've been searching for a way to get the hostname of the web application in the Startup class of my asp.net core 2.0 app. Specifically, in the Configure method I would like to dynamically set a service property based on the hostname.
For instance, I need to know if the host is running as http://servername/myapp or https://externalhost.com/myapp

Thank you for any help

Joe Washek
  • 43
  • 2
  • 8
  • 1
    There is no difference between the two. Your app could technically run as both. Additionally, with an external DNS name, it's not something a server may know at all, but can definitely serve from (highly likely it's unaware of the dns name). – Erik Philips Oct 18 '17 at 21:09
  • The issue I am having is in the in Configure method, I am injecting a log4net configuration file. Being that I have two application pool workers running (internal access, external access), I think they are fighting over the log file. So my thought was on startup, if host name is {whatever}, use my.config, else use my-internal.config – Joe Washek Oct 18 '17 at 21:21
  • As Erik Philips mentioned, you can't know in the Startup-class, which Uri is/will be used, because you can be redirected and have several Dns-Entries for your site, but you can use a action-filter for the Request to get used Uri. – Nikolaus Oct 18 '17 at 21:25
  • This won't solve it any portable way, @Nikolaus. Different hostnames may point to the same application pool/instance – Dag Baardsen Oct 18 '17 at 21:26
  • 1
    @DagBaardsen I wrote the comment, before I read, that the Loading of the File is the issue . – Nikolaus Oct 18 '17 at 21:30
  • @Nikolaus, ah, ok. – Dag Baardsen Oct 18 '17 at 21:31

1 Answers1

2

You cannot distinguish between hostnames, but on Application Pool identities. Use this call to distinguish between identities:

System.Security.Principal.WindowsIdentity.GetCurrent().Name
Dag Baardsen
  • 774
  • 7
  • 9
  • Can this be used in the Startup.cs? – Nikolaus Oct 18 '17 at 21:35
  • Yes it can. Definitely! – Dag Baardsen Oct 18 '17 at 21:38
  • Dag, thanks! This would definitely work, but it led me to another similar approach discussed [link here](https://stackoverflow.com/questions/25664278/how-to-get-application-pool-name-through-code-c-asp-net) Environment.GetEnvironmentVariable("APP_POOL_ID", EnvironmentVariableTarget.Process) https://stackoverflow.com/questions/25664278/how-to-get-application-pool-name-through-code-c-asp-net – Joe Washek Oct 18 '17 at 21:51
  • Yes, that's a nice one, Joe! – Dag Baardsen Oct 18 '17 at 21:55