コード例 #1
0
        public async Task ExecuteAsync()
        {
            var clientUrls    = _configuration.GetSection("IdentityServer:ClientUrls").Get <Dictionary <string, string[]> >();
            var apiSecrets    = _configuration.GetSection("IdentityServer:ApiSecrets").Get <Dictionary <string, string[]> >();
            var clientSecrets = _configuration.GetSection("IdentityServer:ClientSecrets").Get <Dictionary <string, string> >();

            var allClients = _context.Clients.ToList();

            foreach (var client in IdentityConfiguration.GetClients(clientUrls, clientSecrets))
            {
                if (!allClients.Any(t => t.ClientId == client.ClientId))
                {
                    _context.Clients.Add(client.ToEntity());
                }
            }
            await _context.SaveChangesAsync();

            if (!_context.ApiScopes.Any())
            {
                foreach (var scope in IdentityConfiguration.ApiScopes)
                {
                    _context.ApiScopes.Add(scope.ToEntity());
                }
                await _context.SaveChangesAsync();
            }

            if (!_context.IdentityResources.Any())
            {
                foreach (var resource in IdentityConfiguration.IdentityResources)
                {
                    _context.IdentityResources.Add(resource.ToEntity());
                }
                await _context.SaveChangesAsync();
            }

            if (!_context.ApiResources.Any())
            {
                foreach (var api in IdentityConfiguration.GetApis(apiSecrets))
                {
                    _context.ApiResources.Add(api.ToEntity());
                }
                await _context.SaveChangesAsync();
            }
        }
コード例 #2
0
 public void Configure(IWebHostBuilder builder)
 {
     builder.ConfigureServices(services =>
     {
         services.AddControllers();
         services.AddIdentityServer(options =>
         {
             options.Events.RaiseErrorEvents       = true;
             options.Events.RaiseFailureEvents     = true;
             options.Events.RaiseInformationEvents = true;
             options.Events.RaiseSuccessEvents     = true;
         })
         .AddDeveloperSigningCredential()
         .AddInMemoryApiScopes(IdentityConfiguration.ApiScopes)
         .AddInMemoryApiResources(IdentityConfiguration.GetApis())
         .AddInMemoryIdentityResources(IdentityConfiguration.IdentityResources)
         .AddInMemoryClients(IdentityConfiguration.GetClients())
         .AddJwtBearerClientAuthentication();
     });
 }