Beispiel #1
0
        private static IIdentityServerBuilder RegisterIdentityServerStoresWithDbContexts
        <TConfigurationDbContext, TPersistedGrantDbContext>
            (IIdentityServerBuilder builder, IConfiguration configuration, ILogger logger,
            Action <ConfigurationStoreOptions> configurationStoreOptions, Action <OperationalStoreOptions> operationalStoreOptions)
            where TPersistedGrantDbContext : DbContext, IPersistedGrantDbContext
            where TConfigurationDbContext : DbContext, IConfigurationDbContext
        {
            // Config DB from existing connection
            builder.AddConfigurationStore <TConfigurationDbContext>(configurationStoreOptions);

            // Operational DB from existing connection
            builder.AddOperationalStore <TPersistedGrantDbContext>(operationalStoreOptions);

            builder.AddCustomSigningCredential(configuration, logger);
            builder.AddCustomValidationKey(configuration, logger);

            return(builder);
        }
Beispiel #2
0
        public void ConfigureServices(IServiceCollection services)
        {
            string connectionString = Configuration.GetConnectionString("DefaultConnection");

            services.AddTransient <LdapService>();
            services.AddTransient <LdapUserManager>();
            services.AddTransient <LdapSignInManager>();
            services.AddTransient <LdapService>();
            services.AddTransient <UserRepository>();
            services.AddDbContext <ApplicationDbContext>(options =>
            {
                options.UseSqlServer(connectionString);
            });
            string migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddUserManager <UserManager <ApplicationUser> >()
            .AddSignInManager <SignInManager <ApplicationUser> >()
            .AddDefaultTokenProviders();
            services
            .Configure <LdapSettings>(
                Configuration.GetSection("LdapSettings"));
            services.AddCors(options => options.AddPolicy("anyOrigin", p => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials()));
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.Configure <IISOptions>(iis =>
            {
                iis.AuthenticationDisplayName = "Windows";
                iis.AutomaticAuthentication   = false;
            });
            //services.Configure<IISOptions>(iis =>
            //{
            //    iis.AuthenticationDisplayName = "Windows";
            //    iis.AutomaticAuthentication = true;
            //});
            // services.AddTransient<ITokenCreationService, CustomDefaultTokenCreationService>();

            IIdentityServerBuilder identityServerServices = services.AddIdentityServer()

                                                            .AddAspNetIdentity <ApplicationUser>()

                                                            .AddConfigurationStore(options =>
            {
                options.ConfigureDbContext = b =>
                                             b.UseSqlServer(connectionString,
                                                            sql => sql.MigrationsAssembly(migrationsAssembly));
                options.DefaultSchema = string.IsNullOrEmpty(Configuration.GetValue <string>("DatabaseSchema"))? "dbo":Configuration.GetValue <string>("DatabaseSchema");
            })
                                                            // this adds the operational data from DB (codes, tokens, consents)
                                                            .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = b =>
                                             b.UseSqlServer(connectionString,
                                                            sql => sql.MigrationsAssembly(migrationsAssembly));
                options.DefaultSchema = string.IsNullOrEmpty(Configuration.GetValue <string>("DatabaseSchema")) ? "dbo" : Configuration.GetValue <string>("DatabaseSchema");
                // this enables automatic token cleanup. this is optional.
                options.EnableTokenCleanup = true;
            })    // Custom Profile Service for Custom Claims
                                                            .AddProfileService <AspNetIdentityProfileService>()

                                                            //Custom Password Validator
                                                            .AddResourceOwnerValidator <ResourceOwnerPasswordValidator>();


            identityServerServices.AddCustomSigningCredential(Configuration["SigningKey"]);
        }