Asp.Net User Secrets

So I’ve been playing around with Asp.Net 5 beta 5 trying to figure out how to use some of the new configuration stuff. Especially the user secrets stuff.

The idea of user secrets is that you need to have some settings on you dev machine which you don’t want in source control. Things like passwords etc.

I’ve found the documentation some what lacking so hopefully this overview will help.

Firstly you need to install the user-secret command. The installation instructions aren’t too bad and can be found here DNX-Secret-Configuration.

To add the user secrets

Once I had that installed I added the userSecrestsId to my project.json, but that didn’t seem to be picked up.

{  
    "webroot": "wwwroot",  
    "userSecretsId": "aspnet5-4c570577-d4ba-491c-a56c-3f267e3d5211",  
    "version": "1.0.0-*",

I found that I need to add this to the startup.js when I loaded the user secrets.

public class Startup  
    {  
        public Startup(IHostingEnvironment env)  
        {  
            var builder = new ConfigurationBuilder();  
            if (env.IsDevelopment())  
            {  
                builder.AddUserSecrets("aspnet5-4c570577-d4ba-491c-a56c-3f267e3d5211");  
            }  
            else  
            {  
                builder.AddEnvironmentVariables();  
            }  
            Configuration = builder.Build();  
        }

The next problem was figuring out how to inject these values into the controller. What I found is you need to AddOptions in the ConfigureServices method and they Configure the class you want the values put in.

public void ConfigureServices(IServiceCollection services)  
       {  
           services.AddMvc();  
           services.AddOptions();  
           services.ConfigureAwsOptions>(Configuration);

My AwsOptions class just has two string fields with the name of the secret properties I want to use.

namespace Config  
{  
    public class AwsOptions  
    {  
        public string awsAccessKey { get; set; }  
        public string awsSecret { get; set; }          
    }  
}

The ASP.NET configuration system will look for values in the Configuration object that match these name and load them into that class. Then to get them in the controller you do the following

[Route("api/[controller]")]  
public class AController : Controller  
{  
    private readonly IOptionsAwsOptions> _awsOptions;  
    private readonly ILoggerSeverController> _logger;  

    public AController(IOptionsAwsOptions> awsOptions, ILoggerAController> logger)  
    {  
        _awsOptions = awsOptions;  
        _logger = logger;  
    }  

    // POST api/values  
    [HttpPost]  
    public async TaskIActionResult> Post(Guid Id)  
    {  

        AmazonSimpleNotificationServiceClient snsclient =  new AmazonSimpleNotificationServiceClient(_awsOptions.Options.awsAccessKey,  
                 _awsOptions.Options.awsSecret, RegionEndpoint.USWest2);

That’s it. Then you will get your user secret values injected into you class.

Full Startup.cs (because I hate it when the snippets miss something I need)

using Microsoft.AspNet.Builder;  
using Microsoft.AspNet.Hosting;  
using Microsoft.Framework.Configuration;  
using Microsoft.Framework.DependencyInjection;  
using Microsoft.Framework.Logging;  
using Severing.Service.Config;  

namespace Service  
{  
    public class Startup  
    {  
        public Startup(IHostingEnvironment env)  
        {  
            var builder = new ConfigurationBuilder();  
            if (env.IsDevelopment())  
            {  
                builder.AddUserSecrets("aspnet5-4c570577-d4ba-491c-a56c-3f267e3d5211");  
            }  
            else  
            {  
                builder.AddEnvironmentVariables();  
            }  
            Configuration = builder.Build();  
        }  

        public IConfiguration Configuration { get; set; }  

        // This method gets called by a runtime.  
        // Use this method to add services to the container  
        public void ConfigureServices(IServiceCollection services)  
        {  
            services.AddMvc();  
            services.AddOptions();  
            services.ConfigureAwsOptions>(Configuration);  
            services.AddLogging();  
            // Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.  
            // You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.  
            // services.AddWebApiConventions();  
        }  

        // Configure is called after ConfigureServices is called.  
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
        {  

            loggerFactory.AddConsole();  
            // Configure the HTTP request pipeline.  
            app.UseStaticFiles();  

            // Add MVC to the request pipeline.  
            app.UseMvc();  
            // Add the following route for porting Web API 2 controllers.  
            // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");  
        }  
    }  
}