示例#1
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();
                });
            });
        }
示例#2
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);
                }
            }
        }