From ccc257f8fca30c6a0e1fde4c04c1edcf05c83bf1 Mon Sep 17 00:00:00 2001 From: Praburaj Date: Tue, 8 Apr 2014 14:21:19 -0700 Subject: [PATCH] Adding environment based config to the music store project with a Config.json file for local. If the same set of values (those present in config.json) is present in the environment variables then ones from environment variables be picked up. This will be the antares scenario where the portal variables flow in as environment variables with a specific prefix. --- src/MusicStore/Config.json | 4 ++++ src/MusicStore/Startup.cs | 36 ++++++------------------------------ src/MusicStore/project.json | 3 ++- 3 files changed, 12 insertions(+), 31 deletions(-) create mode 100644 src/MusicStore/Config.json diff --git a/src/MusicStore/Config.json b/src/MusicStore/Config.json new file mode 100644 index 0000000000..5959953d17 --- /dev/null +++ b/src/MusicStore/Config.json @@ -0,0 +1,4 @@ +{ + "DefaultAdminUsername": "Administrator", + "DefaultAdminPassword": "YouShouldChangeThisPassword" +} \ No newline at end of file diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index 1b83851077..57ac262197 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -1,6 +1,7 @@ using Microsoft.AspNet; using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.ConfigurationModel; +using Microsoft.AspNet.Configuration.Json; using Microsoft.AspNet.DependencyInjection; using Microsoft.AspNet.DependencyInjection.Fallback; using Microsoft.AspNet.DependencyInjection.NestedProviders; @@ -47,39 +48,14 @@ public class Startup SampleData.InitializeMusicStoreDatabase(); } - //Bug: We need EF to integrate with SQL server. Until then we will use in memory store - //private async void CreateAdminUser() - //{ - // string _username = ConfigurationManager.AppSettings["DefaultAdminUsername"]; - // string _password = ConfigurationManager.AppSettings["DefaultAdminPassword"]; - // string _role = "Administrator"; - - // var context = new ApplicationDbContext(); - // var userManager = new UserManager(new UserStore(context)); - // var roleManager = new RoleManager(new RoleStore(context)); - - // var role = new IdentityRole(_role); - // var result = await roleManager.RoleExistsAsync(_role); - // if (result == false) - // { - // await roleManager.CreateAsync(role); - // } - - // var user = await userManager.FindByNameAsync(_username); - // if (user == null) - // { - // user = new ApplicationUser { UserName = _username }; - // await userManager.CreateAsync(user, _password); - // await userManager.AddToRoleAsync(user.Id, _role); - // } - //} - private async void CreateAdminUser() { - //How to read from local appSettings? var configuration = new Configuration(); - string _username = "Administrator"; // configuration.Get("DefaultAdminUsername"); - string _password = "YouShouldChangeThisPassword"; // configuration.Get("DefaultAdminPassword"); + configuration.AddEnvironmentVariables(); //If configuration flows through environment we should pick that first + configuration.AddJsonFile("Config.json"); + + string _username = configuration.Get("DefaultAdminUsername"); + string _password = configuration.Get("DefaultAdminPassword"); string _role = "Administrator"; var userManager = new UserManager(new InMemoryUserStore()); diff --git a/src/MusicStore/project.json b/src/MusicStore/project.json index c43908837d..754b2a437a 100644 --- a/src/MusicStore/project.json +++ b/src/MusicStore/project.json @@ -25,7 +25,8 @@ "Microsoft.Data.InMemory": "0.1-alpha-*", "Microsoft.AspNet.Diagnostics": "0.1-alpha-*", "Microsoft.AspNet.Hosting": "0.1-alpha-*", - "Microsoft.AspNet.Server.WebListener": "0.1-alpha-*" + "Microsoft.AspNet.Server.WebListener": "0.1-alpha-*", + "Microsoft.AspNet.Configuration.Json": "0.1-alpha-*" }, "commands": { "web": "Microsoft.AspNet.Hosting server.name=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001"