Exemple #1
0
        // 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.Configure <IdentitySettings>(Configuration);
            var settings = Configuration.Get <IdentitySettings>();

            services.AddSingleton(settings);

            services.AddIdentityServer
            (
                options =>
            {
                options.IssuerUri = settings.IdentityEndPoint;
                options.Endpoints.EnableDiscoveryEndpoint = true;
                options.Endpoints.EnableTokenEndpoint     = true;
                options.Endpoints.EnableUserInfoEndpoint  = true;

                options.Endpoints.EnableAuthorizeEndpoint       = false;
                options.Endpoints.EnableCheckSessionEndpoint    = false;
                options.Endpoints.EnableEndSessionEndpoint      = false;
                options.Endpoints.EnableIntrospectionEndpoint   = false;
                options.Endpoints.EnableTokenRevocationEndpoint = false;
            }
            )
            .AddDeveloperSigningCredential()
            .AddInMemoryApiResources(IdentityConfiguration.GetApiResources(settings))
            .AddInMemoryClients(IdentityConfiguration.GetClients(settings));

            services.AddMvc();
        }
Exemple #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <AuthDbContext>(options =>
                                                  options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")));
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "TodoApp.Identity", Version = "v1"
                });
            });

            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <AuthDbContext>()
            //.AddClaimsPrincipalFactory<CustomUserClaimsPrincipalFactory>();
            .AddDefaultTokenProviders();     // Что это делает?

            services.AddIdentityServer(options => options.IssuerUri = "localhost")
            .AddInMemoryApiResources(IdentityConfiguration.GetApiResources())
            .AddInMemoryIdentityResources(IdentityConfiguration.GetIdentityResources())
            .AddInMemoryApiScopes(IdentityConfiguration.GetScopes())
            .AddInMemoryClients(IdentityConfiguration.GetClients())
            .AddAspNetIdentity <ApplicationUser>()
            .AddProfileService <ProfileService>()
            .AddDeveloperSigningCredential(false);

            services.AddAuthentication()
            .AddGoogle("Google", options =>
            {
                options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;

                options.ClientId     = "631863314589-78e0flbpm57l2rg6gi6h7meunj68f4in.apps.googleusercontent.com";
                options.ClientSecret = "LfOYActB2UPuPmW9sho7c_zi";
            });

            services.AddTransient <IUserClaimsPrincipalFactory <ApplicationUser>, CustomUserClaimsPrincipalFactory>();

            services.AddCors(options => {
                options.AddPolicy("default", policy =>
                {
                    policy.WithOrigins("http://localhost:3000")
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
                });
            });
        }
        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();
            }
        }
 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();
     });
 }
        // 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.AddCors(options =>
            {
                options.AddPolicy("AllowAll",
                                  builder => builder
                                  .AllowAnyMethod()
                                  .AllowAnyOrigin()
                                  .AllowAnyHeader());
            });

            services.Configure <IdentitySettings>(Configuration);
            var settings = Configuration.Get <IdentitySettings>();

            services.AddSingleton(settings);

            services.AddIdentityServer
            (
                options =>
            {
                //options.Cors = new CorsOptions{};
                options.IssuerUri = settings.IdentityEndPoint;
                options.Endpoints.EnableDiscoveryEndpoint = true;
                options.Endpoints.EnableTokenEndpoint     = true;
                options.Endpoints.EnableUserInfoEndpoint  = true;

                options.Endpoints.EnableAuthorizeEndpoint       = false;
                options.Endpoints.EnableCheckSessionEndpoint    = false;
                options.Endpoints.EnableEndSessionEndpoint      = false;
                options.Endpoints.EnableIntrospectionEndpoint   = false;
                options.Endpoints.EnableTokenRevocationEndpoint = false;
            }
            )
            //.AddDeveloperSigningCredential()
            .AddDeveloperSigningCredential()
            .AddInMemoryApiResources(IdentityConfiguration.GetApiResources(settings))
            .AddInMemoryClients(IdentityConfiguration.GetClients(settings));

            services.AddMvc();
        }
Exemple #6
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //IdentityConfiguration.Configuration = Configuration;

            services.AddDbContext <TestDBContext>();

            services.AddAutoMapper();

            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryIdentityResources(IdentityConfiguration.GetIdentityResources())
            .AddInMemoryApiResources(IdentityConfiguration.GetApiResources())
            .AddInMemoryClients(IdentityConfiguration.GetClients())
            .AddResourceOwnerValidator <ResourceOwnerPasswordValidator>()
            .AddProfileService <ProfileService>();

            var builder = new ContainerBuilder();

            builder.RegisterModule <UtilModule>();
            builder.RegisterModule <ServiceModule>();
            builder.Populate(services);
            ApplicationContainer = builder.Build();
            return(new AutofacServiceProvider(ApplicationContainer));
        }
        public static void AddIdentityServerConfig(this IServiceCollection services, IConfiguration configuration, IHostingEnvironment env)
        {
            services.AddIdentity <User, IdentityRole>()
            .AddEntityFrameworkStores <ShopContext>()
            .AddDefaultTokenProviders();

            services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryPersistedGrants()
            .AddInMemoryIdentityResources(IdentityConfiguration.GetIdentityResources())
            .AddInMemoryApiResources(IdentityConfiguration.GetApiResources())
            .AddInMemoryClients(IdentityConfiguration.GetClients())
            .AddAspNetIdentity <User>();

            services.AddTransient <IProfileService, IdentityProfileService>();

            services.Configure <IdentityOptions>(config =>
            {
                config.Password.RequireDigit           = true;
                config.Password.RequiredLength         = 8;
                config.Password.RequireUppercase       = false;
                config.Password.RequireNonAlphanumeric = false;
            });

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.Authority            = configuration["ID4:Authority"];
                options.Audience             = configuration["ID4:Audience"];
                options.RequireHttpsMetadata = false;
            });
        }
        public async static Task UseIdentityServerDataAsync(this IApplicationBuilder app, IConfiguration configuration)
        {
            using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                await serviceScope.ServiceProvider.GetRequiredService <IdentityDbContext>().Database.MigrateAsync()
                .ConfigureAwait(false);

                await serviceScope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.MigrateAsync()
                .ConfigureAwait(false);


                var configurationContext = serviceScope.ServiceProvider.GetRequiredService <ConfigurationDbContext>();
                await configurationContext.Database.MigrateAsync()
                .ConfigureAwait(false);

                //Get configuration from db
                var dbClients = await configurationContext.Clients.ToListAsync().ConfigureAwait(false);

                var dbApiScopes = await configurationContext.ApiScopes.ToListAsync().ConfigureAwait(false);

                var dbApiResources = await configurationContext.ApiResources.ToListAsync().ConfigureAwait(false);

                var dbIdentityResources = await configurationContext.IdentityResources.ToListAsync().ConfigureAwait(false);

                //check if some configuration is missing in database if yes insert into database
                foreach (var client in IdentityConfiguration.GetClients())
                {
                    if (!dbClients.Any(x => x.ClientId == client.ClientId))
                    {
                        configurationContext.Clients.Add(client.ToEntity());
                    }
                }

                foreach (var scope in IdentityConfiguration.GetApiScopes())
                {
                    if (!dbApiScopes.Any(x => x.Name == scope.Name))
                    {
                        configurationContext.ApiScopes.Add(scope.ToEntity());
                    }
                }

                foreach (var apiResource in IdentityConfiguration.GetResourceApis())
                {
                    if (!dbApiResources.Any(x => x.Name == apiResource.Name))
                    {
                        configurationContext.ApiResources.Add(apiResource.ToEntity());
                    }
                }

                foreach (var identityResource in IdentityConfiguration.GetIdentityResources())
                {
                    if (!dbIdentityResources.Any(x => x.Name == identityResource.Name))
                    {
                        configurationContext.IdentityResources.Add(identityResource.ToEntity());
                    }
                }

                await configurationContext.SaveChangesAsync()
                .ConfigureAwait(false);

                // seed the Identity Roles (define here which roles application will have)
                var roleManager = serviceScope.ServiceProvider.GetRequiredService <RoleManager <IdentityRole> >();
                if (!roleManager.Roles.Any(r => r.Name.Equals("admin")))
                {
                    await roleManager.CreateAsync(new IdentityRole("admin"))
                    .ConfigureAwait(false);
                }
            }
        }