Skip to content

Instantly share code, notes, and snippets.

@shawnwildermuth
Created August 21, 2015 06:20
Show Gist options
  • Save shawnwildermuth/87cbf15772ec511e4985 to your computer and use it in GitHub Desktop.
Save shawnwildermuth/87cbf15772ec511e4985 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Authentication.Cookies;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Mvc;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Runtime;
using Newtonsoft.Json.Serialization;
using TheWorld.Models;
using TheWorld.Models.Auth;
using TheWorld.Models.Trips;
using TheWorld.Services;
using TheWorld.ViewModels;
namespace TheWorld
{
public class Startup
{
public static IConfiguration Configuration { get; private set; }
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Setup configuration sources.
var configBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
Configuration = configBuilder.Build();
}
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.ConfigureMvcJson(opts =>
{
opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<WorldContext>();
services.AddIdentity<WorldUser, IdentityRole>(config =>
{
config.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<WorldContext>();
//services.Configure<CookieAuthenticationOptions>(opt =>
//{
// opt.LoginPath = PathString.FromUriComponent("/Auth/Login");
//});
#if DEBUG
services.AddScoped<IMailService, DebugMailService>();
#else
//services.AddScoped<IMailService, MailService>();
#endif
services.AddScoped<WorldContextSeedData>();
services.AddScoped<IWorldRepository, WorldRepository>();
services.AddScoped<IGeoCoordsService, GeoCoordsService>();
services.AddLogging();
}
public async void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Debug);
AutoMapper.Mapper.Initialize(config =>
{
config.CreateMap<Stop, StopViewModel>().ReverseMap();
config.CreateMap<Trip, TripViewModel>().ReverseMap();
});
app.UseStaticFiles();
app.MapWhen(x => x.Request.Path.StartsWithSegments("/api"), ApiConfiguration);
app.MapWhen(x => !x.Request.Path.StartsWithSegments("/api"), WebConfiguration);
var seeder = (WorldContextSeedData)app.ApplicationServices.GetService(typeof(WorldContextSeedData));
await seeder.EnsureSampleDataAsync();
}
private void WebConfiguration(IApplicationBuilder app)
{
app.UseIdentity();
app.UseCookieAuthentication(config =>
{
config.AutomaticAuthentication = true;
config.LoginPath = new PathString("/auth/login");
});
app.UseMvc(config =>
{
config.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "App", action = "Index" });
});
}
private void ApiConfiguration(IApplicationBuilder app)
{
app.UseIdentity();
app.UseCookieAuthentication(config =>
{
config.AutomaticAuthentication = true;
config.LoginPath = new PathString("/foo/bar");
});
app.UseMvc();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment