Home
%3CLINGO-SUB%20id%3D%22lingo-sub-392596%22%20slang%3D%22en-US%22%3EASP.NET%20Core%20AppSettings%20for%20Azure%20App%20Service%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-392596%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Jun%2012%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EIn%20regular%20.NET%20world%2C%20we%20have%20option%20to%20save%20settings%20in%20web.config%20or%20in%20app.config%20file.%20But%20in%20.NET%20Core%2C%20you%20have%20option%20to%20save%20them%20in%20few%20other%20locations%20like%20%3CSTRONG%3E%20appsettings.json%20%3C%2FSTRONG%3E%20%2C%20%3CSTRONG%3E%20appsettings.%3CENVIRONMENT%3E.json%20%3C%2FENVIRONMENT%3E%3C%2FSTRONG%3E%20and%20in%20%3CSTRONG%3E%20secrets.json.%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20One%20of%20the%20main%20reason%20settings%20are%20not%20saved%20in%20web.config%20file%20is%2C%20to%20avoid%20settings%20getting%20checked-into%20repo.%20By%20default%20appsettings.json%20file%20are%20ignored%20by%20repo%20clients.%3C%2FP%3E%0A%20%20%3CP%3EMain%20difference%20between%20%3CSTRONG%3E%20appsettings.json%20%3C%2FSTRONG%3E%20and%20%3CSTRONG%3E%20secrets.json%20%3C%2FSTRONG%3E%20is%20the%20file%20location.%20Appsettings.json%20is%20at%20the%20root%20of%20source%20folder%2C%20but%20secrets.json%20file%20is%20at%20this%20location%20%3CSTRONG%3E%20C%3A%5CUsers%5C%3CUSERNAME%3E%5CAppData%5CRoaming%5CMicrosoft%5CUserSecrets%5C%3CPROJECT%20guid%3D%22%22%3E%5Csecrets.json%20%3C%2FPROJECT%3E%3C%2FUSERNAME%3E%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Secrets.json%20file%20is%20available%20only%20for%20that%20specific%20user.%20This%20is%20controlled%20by%20OS.%20You%20can%20create%20this%20secrets.json%20file%20from%20Visual%20Studio%2C%20just%20right%20click%20on%20the%20project%20and%20select%20%3CSTRONG%3E%20Manage%20User%20Secrets%20%3C%2FSTRONG%3E%20.%3C%2FP%3E%0A%20%20%3CP%3ENow%20when%20you%20deploy%20your%20ASP.NET%20Core%20website%20to%20Azure%20App%20Services%2C%20usually%20appsettings.json%20is%20not%20deployed%20(you%20don%E2%80%99t%20want%20others%20to%20download%20your%20secrets).%20So%20you%20have%20to%20use%20Azure%20Portal%20%3CSTRONG%3E%20Application%20Settings%20%3C%2FSTRONG%3E%20blade%20to%20upload%20your%20settings.%20There%20will%20be%20no%20secrets.json%20file%20in%20Azure%20App%20Services.%20In%20Azure%20App%20Services%2C%20you%20can%20use%20either%20use%20Application%20Settings%20or%20Connection%20Strings.%20If%20you%20save%20in%20the%20Connection%20String%2C%20you%20have%20to%20use%20%3CSTRONG%3E%20GetConnectionString()%20%3C%2FSTRONG%3E%20API.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EHere%20is%20a%20quick%20example%2C%20lets%20say%20we%20have%20%3CSTRONG%3E%20secrets.json%20%3C%2FSTRONG%3E%20as%20follows%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20%22Parent%22%3A%20%7B%20%3CBR%20%2F%3E%20%22ChildOne%22%3A%20%22C1%20from%20secrets.json%22%2C%20%3CBR%20%2F%3E%20%22ChildTwo%22%3A%20%22C2%20from%20secrets.json%22%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%3C%2FP%3E%3CBR%20%2F%3E%20And%20%3CSTRONG%3E%20appsettings.json%20%3C%2FSTRONG%3E%20as%20follows%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20%7B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22Parent%22%3A%20%7B%20%3CBR%20%2F%3E%20%22ChildOne%22%3A%20%22C1%20from%20appsettings.json%22%2C%20%3CBR%20%2F%3E%20%22ChildTwo%22%3A%20%22C2%20from%20appsettings.json%22%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%3CP%3EWe%20have%20%3CSTRONG%3E%20controller%20%3C%2FSTRONG%3E%20code%20as%20below%20%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22csharp%22%5D%20%3CBR%20%2F%3E%20public%20class%20HomeController%20%3A%20Controller%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20private%20readonly%20IConfiguration%20Config%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20public%20HomeController(IConfiguration%20config)%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20Config%20%3D%20config%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20public%20IActionResult%20Index()%20%3CBR%20%2F%3E%20%7B%20%3CBR%20%2F%3E%20ViewData%5B%22Parent%3AChildOne%22%5D%20%3D%20Config%5B%22Parent%3AChildOne%22%5D%3B%20%3CBR%20%2F%3E%20ViewData%5B%22Parent%3AChildTwo%22%5D%20%3D%20Config%5B%22Parent%3AChildTwo%22%5D%3B%20%3CBR%20%2F%3E%20ViewData%5B%22ConnectionStrings%3AParent%3AChildTwo%22%5D%20%3D%20Config%5B%22ConnectionStrings%3AParent%3AChildTwo%22%5D%3B%20%3CBR%20%2F%3E%20ViewData%5B%22CUSTOMCONNSTR_Parent%3AChildTwo%22%5D%20%3D%20Config.GetConnectionString(%22Parent%3AChildTwo%22)%3B%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20return%20View()%3B%20%3CBR%20%2F%3E%20%7D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20And%20%3CSTRONG%3E%20view%20%3C%2FSTRONG%3E%20code%20is%20as%20below%20%3A%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20%5Bcode%20language%3D%22html%22%5D%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CH3%20id%3D%22toc-hId-1485073157%22%20id%3D%22toc-hId-1505390622%22%3E%3CBR%20%2F%3E%20(Parent%3AChildOne)%20%40ViewData%5B%22Parent%3AChildOne%22%5D%20%3CBR%20%2F%3E%3C%2FH3%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId--1067083804%22%20id%3D%22toc-hId--1046766339%22%3E%3CBR%20%2F%3E%20(Parent%3AChildTwo)%20%40ViewData%5B%22Parent%3AChildTwo%22%5D%20%3CBR%20%2F%3E%3C%2FH3%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId-675726531%22%20id%3D%22toc-hId-696043996%22%3E%3CBR%20%2F%3E%20(ConnectionStrings%3AParent%3AChildTwo)%20%40ViewData%5B%22ConnectionStrings%3AParent%3AChildTwo%22%5D%20%3CBR%20%2F%3E%3C%2FH3%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId--1876430430%22%20id%3D%22toc-hId--1856112965%22%3E%3CBR%20%2F%3E%20constr%20(Parent%3AChildTwo)%20%40ViewData%5B%22CUSTOMCONNSTR_Parent%3AChildTwo%22%5D%20%3CBR%20%2F%3E%3C%2FH3%3E%0A%20%20%3CP%3E%3CBR%20%2F%3E%20%5B%2Fcode%5D%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EWhen%20you%20run%20locally%20you%20should%20see%20%3A%20%E2%80%9C%20%3CSTRONG%3E%20C1%20from%20secrets.json%20%3C%2FSTRONG%3E%20%E2%80%9D%20as%20shown%20below.%20So%20settings%20are%20read%20from%20the%20%3CSTRONG%3E%20secrets.json%20%3C%2FSTRONG%3E%20file.%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F101198iE8F02B7131D0E72D%22%20%2F%3E%20%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3ENow%20deploy%20it%20to%20Azure%2C%20you%20should%20see%20empty%20values%20as%20shown%20below%20%3A%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F101199iE988D0EC4CCFAAF6%22%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3EAdd%20following%20application%20settings%20and%20connection%20string%20in%20%3CSTRONG%3E%20Azure%20Portal%20%3C%2FSTRONG%3E%20as%20shown%20below.%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F101200i6D1CF4F9DC6AF408%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20When%20you%20hit%20your%20home%20page%20again%20you%20should%20see%20these%20values%20as%20shown%20below.%20Note%3A%20Connection%20Strings%20are%20available%20using%20AppSettings%20also%20by%20just%20adding%20%3CSTRONG%3E%20ConnectionStrings%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F101201i4DBE77AC60673C2A%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Internally%20all%20these%20KEY-VALUE%20pairs%20are%20environment%20variables.%26nbsp%3B%20You%20can%20check%20Azure%20App%20Service%20website%20environment%20variable%20at%20the%20SCM%20website.%20Just%20add%20%3CSTRONG%3E%20.scm%20%3C%2FSTRONG%3E%20to%20your%20website%20URL%20just%20before%20.azurewebsites.net%20and%20click%20on%20%3CSTRONG%3E%20Environment%20%3C%2FSTRONG%3E%20.%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F101202i70421C4141B14FA7%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20And%20Connection%20string%20will%20be%20in%20this%20format%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F101203i652F065ADE3AA926%22%20%2F%3E%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-392596%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Jun%2012%2C%202018%20In%20regular%20.%3C%2FLINGO-TEASER%3E
Microsoft
First published on MSDN on Jun 12, 2018

In regular .NET world, we have option to save settings in web.config or in app.config file. But in .NET Core, you have option to save them in few other locations like appsettings.json , appsettings.<Environment>.json and in secrets.json.
One of the main reason settings are not saved in web.config file is, to avoid settings getting checked-into repo. By default appsettings.json file are ignored by repo clients.

Main difference between appsettings.json and secrets.json is the file location. Appsettings.json is at the root of source folder, but secrets.json file is at this location C:\Users\<username>\AppData\Roaming\Microsoft\UserSecrets\<project GUID>\secrets.json

Secrets.json file is available only for that specific user. This is controlled by OS. You can create this secrets.json file from Visual Studio, just right click on the project and select Manage User Secrets .

Now when you deploy your ASP.NET Core website to Azure App Services, usually appsettings.json is not deployed (you don’t want others to download your secrets). So you have to use Azure Portal Application Settings blade to upload your settings. There will be no secrets.json file in Azure App Services. In Azure App Services, you can use either use Application Settings or Connection Strings. If you save in the Connection String, you have to use GetConnectionString() API.


Here is a quick example, lets say we have secrets.json as follows



[code language="csharp"]
{
"Parent": {
"ChildOne": "C1 from secrets.json",
"ChildTwo": "C2 from secrets.json"
}
}

[/code]


And appsettings.json as follows [code language="csharp"]
{     "Parent": {
"ChildOne": "C1 from appsettings.json",
"ChildTwo": "C2 from appsettings.json"
}
}
}
[/code]

We have controller code as below :

[code language="csharp"]
public class HomeController : Controller
{
private readonly IConfiguration Config;

public HomeController(IConfiguration config)
{
Config = config;
}

public IActionResult Index()
{
ViewData["Parent:ChildOne"] = Config["Parent:ChildOne"];
ViewData["Parent:ChildTwo"] = Config["Parent:ChildTwo"];
ViewData["ConnectionStrings:Parent:ChildTwo"] = Config["ConnectionStrings:Parent:ChildTwo"];
ViewData["CUSTOMCONNSTR_Parent:ChildTwo"] = Config.GetConnectionString("Parent:ChildTwo");

return View();
}

[/code]

And view code is as below :

[code language="html"]


(Parent:ChildOne) @ViewData["Parent:ChildOne"]



(Parent:ChildTwo) @ViewData["Parent:ChildTwo"]



(ConnectionStrings:Parent:ChildTwo) @ViewData["ConnectionStrings:Parent:ChildTwo"]



constr (Parent:ChildTwo) @ViewData["CUSTOMCONNSTR_Parent:ChildTwo"]


[/code]

When you run locally you should see : “ C1 from secrets.json ” as shown below. So settings are read from the secrets.json file.


Now deploy it to Azure, you should see empty values as shown below :

Add following application settings and connection string in Azure Portal as shown below.




When you hit your home page again you should see these values as shown below. Note: Connection Strings are available using AppSettings also by just adding ConnectionStrings:




Internally all these KEY-VALUE pairs are environment variables.  You can check Azure App Service website environment variable at the SCM website. Just add .scm to your website URL just before .azurewebsites.net and click on Environment .




And Connection string will be in this format