public void Run(string environmentName) { // IdentityResource _context.RemoveRange(_context.IdentityResources); _context.SaveChanges(); foreach (var item in IdentityResourceData.GetList(environmentName)) { _context.IdentityResources.Add(item.ToEntity()); } _context.SaveChanges(); // ApiResource _context.RemoveRange(_context.ApiResources); _context.SaveChanges(); foreach (var item in ApiResourceData.GetList(environmentName)) { _context.ApiResources.Add(item.ToEntity()); } _context.SaveChanges(); // Client _context.RemoveRange(_context.Clients); _context.SaveChanges(); foreach (var item in ClientData.GetList(environmentName)) { _context.Clients.Add(item.ToEntity()); } _context.SaveChanges(); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddDbContext <ApplicationDbContext>(options => { options.UseSqlServer(_configuration["ConnectionString"]); }); services.AddIdentity <ApplicationUser, IdentityRole>() .AddEntityFrameworkStores <ApplicationDbContext>() .AddDefaultTokenProviders(); if (_environment.EnvironmentName != "Offline") { services.AddDataProtectionWithSqlServerForIdentityService(_configuration); } services.AddHsts(opts => { opts.IncludeSubDomains = true; opts.MaxAge = TimeSpan.FromSeconds(15768000); }); services.AddControllersWithViews(); var builder = services.AddIdentityServer(options => { options.Events.RaiseErrorEvents = true; options.Events.RaiseFailureEvents = true; options.Events.RaiseInformationEvents = true; options.Events.RaiseSuccessEvents = true; }) .AddInMemoryIdentityResources(IdentityResourceData.Resources()) .AddInMemoryApiResources(ApiResourceData.Resources()) .AddInMemoryApiScopes(ApiScopeData.Resources()) .AddInMemoryClients(ClientData.GetClients()) .AddOperationalStore(options => { options.EnableTokenCleanup = true; //The number of records to remove at a time. Defaults to 100. options.TokenCleanupBatchSize = 100; options.TokenCleanupInterval = 3600; //Seconds options.ConfigureDbContext = b => { options.ConfigureDbContext = c => c.UseSqlServer(_configuration["ConnectionString"]); }; }) .AddAspNetIdentity <ApplicationUser>(); if (_environment.EnvironmentName != "Offline") { builder.AddProductionSigningCredential(_configuration); } else { builder.AddDeveloperSigningCredential(); } }