Get Azure Web App name in code - azure

Is there any way to obtain the name of the web app from within code. i.e. if I have deployed my app to a web app called my-webapp-dev-ne I need to be able to get this name in code. The reason I cannot add it to an app config file or any other method is that the same app can be deployed to multiple places.

Adding to Rick's answer, you can find more informative environment settings here
WEBSITE_SKU - The sku of the site (Possible values: Free, Shared, Basic, Standard).
WEBSITE_COMPUTE_MODE - Specifies whether website is on a dedicated or shared VM/s (Possible values: Shared, Dedicated).
WEBSITE_SITE_MODE - The mode for the site (can be Limited for a free site, Basic for a shared site or empty for a standard site).
WEBSITE_HOSTNAME - The Azure Website's primary host name for the site (For example: site.azurewebsites.net). Note that custom hostnames are not accounted for here.
WEBSITE_INSTANCE_ID - The id representing the VM that the site is running on (If site runs on multiple instances, each instance will have a different id).
WEBSITE_NODE_DEFAULT_VERSION - The default node version this website is using.
WEBSOCKET_CONCURRENT_REQUEST_LIMIT - The limit for websocket's concurrent requests.

The web app environment provides an environment variable called WEBSITE_SITE_NAME that contains the name. You can access it from C# code like this.
var siteName = Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME");

You can do this if you installed the Microsoft.WindowsAzure.ConfigurationManager nuget.
var siteName = CloudConfigurationManager.GetSetting("WEBSITE_SITE_NAME");
Reference:
CloudConfigurationManager Class

I would set this as a configuration in your web.config - for example:
<appSettings>
<add key="sitename" value="my-webapp-dev-ne" />
</appSettings>

Related

Lookup Azure application name from within a running function app

Is it possible to lookup the application name for an Azure app as it runs, i.e., get the information about that is displayed in the Azure portal? In the example below, I'd want something to tell me from within the application that I am running sitemap-prod-eastus.
I've been looking at the Azure Context object but not seeing what I need. There is an invocation ID, a name for the function, a directory - not the info in this window.
Maybe this can be done through Azure Application Insights?
I am working in Node JS.
I've not seen anything that would expose this to a function app. That said, there is one sort of workaround that you could do which would work - go to the Configuration blade for the function app, Application settings tab, and add a configuration key like function_name and set its value to the name of your app. Your app could then just read it out of configuration.
It's an extra step, but if you're doing it with something like ARM or Terraform, it's just another configuration entry with a variable you already declared to set up the app in the first place.
Answering my own question: Azure provides WEBSITE_SITE_NAME in the runtime environment that matches the name of the function app.

How can I view the final appSettings values on an Azure App Service web app?

I have an ASP.NET MVC app deployed to Microsoft Azure App Service and am having some trouble with the appSettings and connectionStrings values.
I have some values set in the web.config and some values overriding them in the Application Settings tab of the App Service. I want to quickly and easily view the final values to check that the settings are being picked up correctly.
How can I do this?
Note: I've tried using az webapp config appsettings list but this only seems to bring back what is configured in the Application Settings of the App Service and not the merged results of combining with web.config.
No Azure API will return values that include settings that come from your web.config file.
The only way to get this is to ask the config system within your own runtime. e.g. Use code along these lines:
foreach (string name in ConfigurationManager.AppSettings)
{
string val = ConfigurationManager.AppSettings[name];
...
}
foreach (ConnectionStringSettings settings in ConfigurationManager.ConnectionStrings)
{
string connStr = settings.ConnectionString;
string provider = settings.ProviderName;
...
}
This will give you the effective values that are applied to your app.
You may also use the following blades in Azure Portal (under Development Tools section):
Console
In order to see the file, you may use type command, e.g.:
type web.config
Advanced Tools
This points to the Kudu service.
You may see files deployed when navigating to Debug Console > Choose either CMD or PowerShell. Then navigate to your config directory (e.g. site/wwwroot) and choose to either download or edit file.
App Service Editor
App Service Editor is a relatively new tool in Azure toolset. Default view is a list of files, so you can browse all hosted files, including configuration ones.
You can view all of your runtime appSettings, connection strings and environment variables (and more..) using azure KUDU SCM. if your application address is "https://app_name.azurewebsites.net" you can access it in the address "https://app_name.scm.azurewebsites.net" or from azure portal
With kudo REST API, you can get the settings, delete or post them in this address https://app_name.scm.azurewebsites.net/api/settings
kudo wiki

How do deploy a public Github project that contains secret API keys to Azure?

I have a project which I plan to open-source at some point. Currently I keep all of my API keys in a class which is not checked in - I have just linked the project to Azure but the API key class not being present causes the deployment to fail.
How can I hide secret API keys in a public project and still have it deploy to Azure?
I have read quite a few posts (this one for instance) but cannot find a solution that allows me to do what I want - does anyone know what I should do here? Is it possible?
For an Azure Web App, you can specify config values on the Configure tab in the portal (under "app settings"). These will override values specified in your Web.config file.
This means you can leave these settings out of your public repository.
For developing locally, you can put the settings in a separate XML file. To do this, update the appSettings in your Web.config like this:
<appSettings file="mysettings.xml" />
Then create mysettings.xml and put your actual settings in a new <appSettings> element there.
You can then add mysettings.xml to your .gitignore file so it won't be checked in to your public repository.
Azure doesn't mind that your mysettings.xml file doesn't exist, and will pick up the settings you specify in the portal instead.

Is there a way to specify a machine key for Azure Websites without specifying it in the web.config?

I want to specify a custom machine key for my websites running on Azure, so I can swap between staging and production and keep the environment consistent between the two without users being "logged out" whenever I do a swap (because otherwise the machine key changes and the user's cookies can't be decrypted anymore). I've previously been setting this in the web.config file, but I don't really like having this value stored in source control (I'm continuously deploying changes to the server). Connection strings can be specified in the Azure portal to avoid this problem. Is there a solution for machine keys?
In your web.config reference an external config file for the machinekey section:
<system.web >
<machineKey configSource="mkey.config"/>
</system.web>
Create a file mkey.config like this:
<machineKey
validationKey="32E35872597989D14CC1D5D9F5B1E94238D0EE32CF10AA2D2059533DF6035F4F"
decryptionKey="B179091DBB2389B996A526DE8BCD7ACFDBCAB04EF1D085481C61496F693DF5F4" />
Upload the mkey.config file to Azure web site using ftp instead of web deploy.
I'm not 100% sure I understand you question correctly, so I'll answer both possible interpretations.
Interpretation #1: Before you swap prod and stage your users got key A when they were accessing the (old) prod. When you do the swap you want users to keep getting key A when they hit the new prod.
Use App Settings. You can set them using the Portal or Powershell. Those are key value strings that you can set and they are accessible as environment variables from your site. When you swap you prod and stage slots the app settings that were on the old prod all move to the new prod, so your customers will see the same values for them.
Interpretation #2: Before you swap prod and stage your users got key A when they were accessing the (old) prod, and key B when the accessed the old staging slot. When you do the swap you want users to getting key B when they hit the new prod and key A when the access the new staging slot
Using sticky settings. Those are app settings that you set for the site but you configure them to stay with the site that they were on, meaning that when you swap sites you swap the settings as well. You can make app settings sticky by using the following powershell command.
Set-AzureWebsite -Name mysite -SlotStickyAppSettingNames #("myslot", "myslot2")
Full details in this link: http://blog.amitapple.com/post/2014/11/azure-websites-slots/#.VMftXHl0waU

How can I set the deployment path for an iisApp Provider using MsDeploy?

I am creating Web Deployment Package zip files for my Web Applications
I found I am able to specify the Site Name of the application by including a pubxml during the packaging of my site and using the PublishProfile property during the build to specify that profile.
The pubxml has <DeployIisAppPath>WebSiteName</DeployIisAppPath> in it and that creates the site name as seen in the IIS Management screen.
I'd like to specify the location on disk of the website when it is deployed to a server that doesn't already have this app installed.
What property can I use to do this?
I see that I could use -replace arguments as shown here: Specify different path for provider iisApp when creating package with msdeploy but I would rather the value be set in my pubxml.
You have two options:
Choose the checkbox for "Include IIS settings ...". When you do this a text box will light up with the title "physical Path of web application on destination server". Using the value for the parameter that is generated for this you can modify the destination app path.
Another option for you is to use msdeploy.exe directly and recreate the package using "apphostconfig" provider instead of iisApp provider and then parameterize the application path on the destination server.

Resources