using System.Linq; using CryptoHelper; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Data.Entity; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Mvc.Server.Models; using Mvc.Server.Services; using OpenIddict; using OpenIddict.Models; namespace Mvc.Server { public class Startup { public void ConfigureServices(IServiceCollection services) { var configuration = new ConfigurationBuilder() .AddJsonFile("config.json") .AddEnvironmentVariables() .Build(); services.AddMvc(); services.AddEntityFramework() .AddSqlServer() .AddDbContext(options => options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"])); services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders() .AddOpenIddict(); services.AddTransient(); services.AddTransient(); } public void Configure(IApplicationBuilder app) { var factory = app.ApplicationServices.GetRequiredService(); factory.AddConsole(); app.UseStaticFiles(); // Add a middleware used to validate access // tokens and protect the API endpoints. app.UseJwtBearerAuthentication(options => { options.Audience = "http://localhost:54540/"; options.Authority = "http://localhost:54540/"; options.RequireHttpsMetadata = false; }); app.UseIdentity(); app.UseGoogleAuthentication(options => { options.ClientId = "560027070069-37ldt4kfuohhu3m495hk2j4pjp92d382.apps.googleusercontent.com"; options.ClientSecret = "n2Q-GEw9RQjzcRbU3qhfTj8f"; }); app.UseTwitterAuthentication(options => { options.ConsumerKey = "6XaCTaLbMqfj6ww3zvZ5g"; options.ConsumerSecret = "Il2eFzGIrYhz6BWjYhVXBPQSfZuS4xoHpSSyD9PI"; }); // Note: OpenIddict must be added after // ASP.NET Identity and the external providers. app.UseOpenIddict(); app.UseMvcWithDefaultRoute(); using (var context = app.ApplicationServices.GetRequiredService()) { context.Database.EnsureCreated(); // Add Mvc.Client to the known applications. if (!context.Applications.Any()) { context.Applications.Add(new Application { ApplicationID = "myClient", DisplayName = "My client application", RedirectUri = "http://localhost:53507/signin-oidc", LogoutRedirectUri = "http://localhost:53507/", Secret = Crypto.HashPassword("secret_secret_secret"), Type = OpenIddictConstants.ApplicationTypes.Confidential }); context.SaveChanges(); } } } } }