using System; using Microsoft.AspNet.Authorization; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Diagnostics; using Microsoft.AspNet.Diagnostics.Entity; using Microsoft.AspNet.Http; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Data.Entity; using Microsoft.Framework.Caching.Memory; using Microsoft.Framework.Configuration; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Logging; using Microsoft.Dnx.Runtime; using MusicStore.Components; using MusicStore.Models; namespace MusicStore { public class Startup { private readonly Platform _platform; public Startup(IApplicationEnvironment env, IRuntimeEnvironment runtimeEnvironment) { //Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1' is found in both the registered sources, //then the later source will win. By this way a Local config can be overridden by a different setting while deployed remotely. var builder = new ConfigurationBuilder(env.ApplicationBasePath) .AddJsonFile("config.json") .AddEnvironmentVariables(); //All environment variables in the process's context flow in as configuration values. Configuration = builder.Build(); _platform = new Platform(runtimeEnvironment); } public IConfiguration Configuration { get; private set; } public void ConfigureServices(IServiceCollection services) { services.Configure(Configuration.GetSection("AppSettings")); var useInMemoryStore = !_platform.IsRunningOnWindows || _platform.IsRunningOnMono || _platform.IsRunningOnNanoServer; // Add EF services to the services container if (useInMemoryStore) { services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext(options => options.UseInMemoryDatabase()); } else { services.AddEntityFramework() .AddSqlServer() .AddDbContext(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])); } // Add Identity services to the services container services.AddIdentity(options => { options.Cookies.ApplicationCookie.AccessDeniedPath = new PathString("/Home/AccessDenied"); }) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); services.AddFacebookAuthentication(options => { options.AppId = "550624398330273"; options.AppSecret = "10e56a291d6b618da61b1e0dae3a8954"; }); services.AddGoogleAuthentication(options => { options.ClientId = "977382855444.apps.googleusercontent.com"; options.ClientSecret = "NafT482F70Vjj_9q1PU4B0pN"; }); services.AddTwitterAuthentication(options => { options.ConsumerKey = "9J3j3pSwgbWkgPFH7nAf0Spam"; options.ConsumerSecret = "jUBYkQuBFyqp7G3CUB9SW3AfflFr9z3oQBiNvumYy87Al0W4h8"; }); services.AddMicrosoftAccountAuthentication(options => { options.Caption = "MicrosoftAccount - Requires project changes"; options.ClientId = "000000004012C08A"; options.ClientSecret = "GaMQ2hCnqAC6EcDLnXsAeBVIJOLmeutL"; }); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => { builder.WithOrigins("http://example.com"); }); }); // Add MVC services to the services container services.AddMvc(); //Add InMemoryCache services.AddSingleton(); // Add session related services. services.AddCaching(); services.AddSession(); // Add the system clock service services.AddSingleton(); // Configure Auth services.Configure(options => { options.AddPolicy("ManageStore", new AuthorizationPolicyBuilder().RequireClaim("ManageStore", "Allowed").Build()); }); } //This method is invoked when ASPNET_ENV is 'Development' or is not defined //The allowed values are Development,Staging and Production public void ConfigureDevelopment(IApplicationBuilder app, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(minLevel: LogLevel.Warning); // StatusCode pages to gracefully handle status codes 400-599. app.UseStatusCodePagesWithRedirects("~/Home/StatusCodePage"); // Display custom error page in production when error occurs // During development use the ErrorPage middleware to display error information in the browser app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(DatabaseErrorPageOptions.ShowAll); // Add the runtime information page that can be used by developers // to see what packages are used by the application // default path is: /runtimeinfo app.UseRuntimeInfoPage(); Configure(app); } //This method is invoked when ASPNET_ENV is 'Staging' //The allowed values are Development,Staging and Production public void ConfigureStaging(IApplicationBuilder app, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(minLevel: LogLevel.Warning); // StatusCode pages to gracefully handle status codes 400-599. app.UseStatusCodePagesWithRedirects("~/Home/StatusCodePage"); app.UseExceptionHandler("/Home/Error"); Configure(app); } //This method is invoked when ASPNET_ENV is 'Production' //The allowed values are Development,Staging and Production public void ConfigureProduction(IApplicationBuilder app, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(minLevel: LogLevel.Warning); // StatusCode pages to gracefully handle status codes 400-599. app.UseStatusCodePagesWithRedirects("~/Home/StatusCodePage"); app.UseExceptionHandler("/Home/Error"); Configure(app); } public void Configure(IApplicationBuilder app) { // Configure Session. app.UseSession(); // Add static files to the request pipeline app.UseStaticFiles(); // Add cookie-based authentication to the request pipeline app.UseIdentity(); app.UseFacebookAuthentication(); app.UseGoogleAuthentication(); app.UseTwitterAuthentication(); // The MicrosoftAccount service has restrictions that prevent the use of http://localhost:5001/ for test applications. // As such, here is how to change this sample to uses http://ktesting.com:5001/ instead. // Edit the Project.json file and replace http://localhost:5001/ with http://ktesting.com:5001/. // From an admin command console first enter: // notepad C:\Windows\System32\drivers\etc\hosts // and add this to the file, save, and exit (and reboot?): // 127.0.0.1 ktesting.com // Then you can choose to run the app as admin (see below) or add the following ACL as admin: // netsh http add urlacl url=http://ktesting:5001/ user=[domain\user] // The sample app can then be run via: // dnx . web app.UseMicrosoftAccountAuthentication(); // Add MVC to the request pipeline app.UseMvc(routes => { routes.MapRoute( name: "areaRoute", template: "{area:exists}/{controller}/{action}", defaults: new { action = "Index" }); routes.MapRoute( name: "default", template: "{controller}/{action}/{id?}", defaults: new { controller = "Home", action = "Index" }); routes.MapRoute( name: "api", template: "{controller}/{id?}"); }); //Populates the MusicStore sample data SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait(); } } }