Created
July 10, 2017 08:51
-
-
Save abel-masila/680f96485d7347e32fc842dc1d7092bc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | |
using Server.Data; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.AspNetCore.Identity; | |
using System.Linq; | |
using IdentityServer4.EntityFramework.DbContexts; | |
using IdentityServer4.EntityFramework.Mappers; | |
using System.Reflection; | |
namespace Server | |
{ | |
public class Startup | |
{ | |
public Startup(IHostingEnvironment env) | |
{ | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(env.ContentRootPath) | |
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) | |
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) | |
.AddEnvironmentVariables(); | |
Configuration = builder.Build(); | |
} | |
public IConfigurationRoot Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// Add framework services. | |
services.AddIdentity<IdentityUser, IdentityRole>() | |
.AddEntityFrameworkStores<ApplicationDbContext>() | |
.AddDefaultTokenProviders(); | |
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name; | |
var connection = @"Data Source=DESKTOP-OK0SFBV;Initial Catalog=aspCore;Persist Security Info=True;User ID=sa ;Password=softwareDD2030"; | |
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connection)); | |
// Add framework services. | |
services.AddIdentityServer() | |
//.AddInMemoryApiResources(Config.GetApiResources()) | |
//.AddInMemoryClients(Config.GetClients()) | |
//.AddInMemoryIdentityResources(Config.GetIdentityResources()) | |
//.AddTestUsers(Config.GetUsers()); | |
.AddOperationalStore(store => store.UseSqlServer(connection, options => options.MigrationsAssembly(migrationsAssembly))) | |
.AddConfigurationStore(store => store.UseSqlServer(connection, options => options.MigrationsAssembly(migrationsAssembly))) | |
.AddTemporarySigningCredential(); | |
services.AddMvc(); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | |
{ | |
loggerFactory.AddConsole(Configuration.GetSection("Logging")); | |
loggerFactory.AddDebug(); | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
app.UseBrowserLink(); | |
} | |
else | |
{ | |
app.UseExceptionHandler("/Home/Error"); | |
} | |
app.UseStaticFiles(); | |
app.UseIdentityServer(); | |
app.UseIdentity(); | |
InitializeDbTestData(app); | |
app.UseMvc(routes => | |
{ | |
routes.MapRoute( | |
name: "default", | |
template: "{controller=Home}/{action=Index}/{id?}"); | |
}); | |
} | |
private static void InitializeDbTestData(IApplicationBuilder app) | |
{ | |
using (var scope = app.ApplicationServices | |
.GetService<IServiceScopeFactory>().CreateScope()) | |
{ | |
scope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>() | |
.Database.Migrate(); | |
scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>() | |
.Database.Migrate(); | |
scope.ServiceProvider.GetRequiredService<ApplicationDbContext>() | |
.Database.Migrate(); | |
var context = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>(); | |
foreach (var client in Config.GetClients()) | |
{ | |
if (context.Clients.FirstOrDefault(c => c.ClientId == client.ClientId) == null) | |
context.Clients.Add(client.ToEntity()); | |
} | |
context.SaveChanges(); | |
if (!context.IdentityResources.Any()) | |
{ | |
foreach (var resource in Config.GetIdentityResources()) | |
{ | |
context.IdentityResources.Add(resource.ToEntity()); | |
} | |
context.SaveChanges(); | |
} | |
if (!context.ApiResources.Any()) | |
{ | |
foreach (var resource in Config.GetApiResources()) | |
{ | |
context.ApiResources.Add(resource.ToEntity()); | |
} | |
context.SaveChanges(); | |
} | |
var usermanager = scope.ServiceProvider.GetRequiredService<UserManager<IdentityUser>>(); | |
if (!usermanager.Users.Any()) | |
{ | |
foreach (var inMemoryUser in Config.GetUsers()) | |
{ | |
var identityUser = new IdentityUser(inMemoryUser.Username) | |
{ | |
Id = inMemoryUser.SubjectId | |
}; | |
foreach (var claim in inMemoryUser.Claims) | |
{ | |
identityUser.Claims.Add(new IdentityUserClaim<string> | |
{ | |
UserId = identityUser.Id, | |
ClaimType = claim.Type, | |
ClaimValue = claim.Value, | |
}); | |
} | |
usermanager.CreateAsync(identityUser, "Password123!").Wait(); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment