Пример #1
0
 // This method gets called by the runtime. Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
     DefaultInboundClaimTypeMap.Clear();
     DefaultOutboundClaimTypeMap.Clear();
     services.AddApplicationInsightsTelemetry(options =>
     {
     });
     services.AddMvc(setupAction =>
     {
         setupAction.EnableEndpointRouting = false;
         setupAction.Filters.Add <ModelStateActionFilter>();
     }).SetCompatibilityVersion(Latest);
     services.Configure <RedisOptions>(_redisOptionsSection);
     services.AddSingleton <IConnectionMultiplexer>(sp =>
     {
         var redisOptions = sp.GetRequiredService <IOptions <RedisOptions> >().Value;
         var options      = new ConfigurationOptions
         {
             Password = redisOptions.Password
         };
         options.EndPoints.Add(redisOptions.Host, redisOptions.Port);
         return(Connect(options));
     });
     services.AddMemoryCache(setupAction =>
     {
     });
     services.Configure <MemoryCacheEntryOptions>(_distributedCacheEntryOptionsSection);
     services.AddMediatR(new[] { typeof(Asset) }, _mediatRSection, configureService => configureService.AsScoped());
     services.AddScoped <INotificationHandler <CreateNotification <Asset, Guid> >, NotificationHandlers.NotificationHandler <Asset, Guid> >();
     services.AddScoped <INotificationHandler <CreateRangeNotification <Asset, Guid> >, NotificationHandlers.NotificationHandler <Asset, Guid> >();
     services.AddScoped <INotificationHandler <UpdateNotification <Asset, Guid> >, NotificationHandlers.NotificationHandler <Asset, Guid> >();
     services.AddScoped <INotificationHandler <UpdateRangeNotification <Asset, Guid> >, NotificationHandlers.NotificationHandler <Asset, Guid> >();
     services.AddScoped <INotificationHandler <DeleteNotification <Guid> >, NotificationHandlers.NotificationHandler <Asset, Guid> >();
     services.AddScoped <INotificationHandler <DeleteRangeNotification <Guid> >, NotificationHandlers.NotificationHandler <Asset, Guid> >();
     services.AddScoped <INotificationHandler <GetRangeNotification <Asset, Guid> >, NotificationHandlers.NotificationHandler <Asset, Guid> >();
     services.AddSingleton(typeof(JsonConverter <>), typeof(JsonElementConverter <>));
     services.AddMongo(_mongoSection);
     services.AddSwagger(_swaggerSection);
     services.AddODataApiExplorer(setupAction =>
     {
         var getAssetsMethod = typeof(AssetsController).GetMethod(nameof(AssetsController.GetAssets));
         setupAction.QueryOptions.Controller <AssetsController>().Action(getAssetsMethod !).Allow(All).AllowTop(default);
Пример #2
0
 // This method gets called by the runtime. Use this method to add services to the container.
 public void ConfigureServices(IServiceCollection services)
 {
     DefaultInboundClaimTypeMap.Clear();
     DefaultOutboundClaimTypeMap.Clear();
     services
     .AddApplicationInsightsTelemetry(
         options: options =>
     {
     })
     .AddRazorPages(
         configure: options =>
     {
     })
     .AddMvcOptions(
         setupAction: options =>
     {
         options.Filters.Add(new ModelStatePageFilter());
     })
     .SetCompatibilityVersion(Latest)
     .Services
     .AddCors(
         setupAction: options =>
     {
     })
     .Configure <CookieTempDataProviderOptions>(
         configureOptions: options =>
     {
         options.Cookie.IsEssential = true;
     })
     .AddEntityFrameworkDataService <IdentityServerDbContext>(
         configureOptions: options =>
     {
     },
         optionsAction: options =>
     {
         options.UseSqlServer(
             connectionString: _entityFrameworkConnectionString,
             sqlServerOptionsAction: sqlServerOptions =>
         {
             sqlServerOptions.MigrationsAssembly("Authentication.Api");
         });
     })
     .AddIdentityServer(
         config: _identityServerOptionsSection,
         isDevelopment: _environment.IsDevelopment(),
         identityOptionsAction: options =>
     {
         options.SignIn.RequireConfirmedAccount = true;
     },
         identityServerOptionsAction: options =>
     {
     },
         configurationStoreOptionsAction: options =>
     {
     },
         operationalStoreOptionsAction: options =>
     {
         options.EnableTokenCleanup = true;
     },
         authenticationOptionsAction: options =>
     {
     },
         identityServerAuthenticationOptionsAction: options =>
     {
         options.Authority = _authority;
         options.ApiName   = _audience;
     },
         facebookOptionsAction: options =>
     {
         options.AppId     = _facebookAppId;
         options.AppSecret = _facebookAppSecret;
         options.Events.OnRemoteFailure = context => context.HandleRemoteFailure();
     })
     .AddDatabaseDeveloperPageExceptionFilter()
     .AddHealthChecks()
     .AddDbContextCheck <IdentityServerDbContext>()
     .Services
     .AddQueueClient(_serviceBusOptionsSection)
     .AddSendGridEmailService(_emailOptionsSection)
     .AddKendo();
 }
Пример #3
0
        private static IServiceCollection AddIdentityServer(
            this IServiceCollection services,
            Core.IdentityServerOptions identityServerOptions,
            bool isDevelopment,
            Action <IdentityServer4.Configuration.IdentityServerOptions> identityServerOptionsAction,
            Action <ConfigurationStoreOptions> configurationStoreOptionsAction,
            Action <OperationalStoreOptions> operationalStoreOptionsAction,
            Action <IdentityOptions> identityOptionsAction,
            Action <AspNetCore.Authentication.AuthenticationOptions> authenticationOptionsAction,
            Action <IdentityServerAuthenticationOptions> identityServerAuthenticationOptionsAction,
            Action <FacebookOptions> facebookOptionsAction)
        {
            DefaultInboundClaimTypeMap.Clear();
            DefaultOutboundClaimTypeMap.Clear();

            var builder = new IdentityServerBuilder(services);

            identityServerOptionsAction = identityServerOptionsAction ?? (_ =>
            {
            });
            builder.Services.Configure <SecurityStampValidatorOptions>(opts =>
            {
                opts.OnRefreshingPrincipal = UpdatePrincipal;
            });
            builder.Services.ConfigureApplicationCookie(options =>
            {
                options.Cookie.IsEssential = true;
                options.Cookie.SameSite    = None;
            });
            builder.Services.ConfigureExternalCookie(options =>
            {
                options.Cookie.IsEssential = true;
                options.Cookie.SameSite    = None;
            });
            builder.Services.Configure <CookieAuthenticationOptions>(TwoFactorRememberMeScheme, options =>
            {
                options.Cookie.IsEssential = true;
            });
            builder.Services.Configure <CookieAuthenticationOptions>(TwoFactorUserIdScheme, options =>
            {
                options.Cookie.IsEssential = true;
            });
            builder.Services.Configure(identityServerOptionsAction);
            builder.Services.AddIdentityServer();
            if (identityServerOptions.UseEntityFramework)
            {
                builder.Services.AddEntityFramework(configurationStoreOptionsAction, operationalStoreOptionsAction);
                builder.AddConfigurationStore <IdentityServerDbContext>(configurationStoreOptionsAction);
                builder.AddOperationalStore <IdentityServerDbContext>(operationalStoreOptionsAction);
            }

            if (identityServerOptions.UseIdentity)
            {
                builder.Services.AddIdentity(identityServerOptions, identityOptionsAction);
                builder.AddAspNetIdentity <User>();
            }

            if (identityServerOptions.UseAuthentication)
            {
                builder.Services.AddAuthentication(
                    identityServerOptions,
                    authenticationOptionsAction,
                    identityServerAuthenticationOptionsAction,
                    facebookOptionsAction);
            }

            if (isDevelopment)
            {
                builder.AddDeveloperSigningCredential();
            }
            else
            {
                if (!IsNullOrWhiteSpace(identityServerOptions.SigningCredential))
                {
                    var rawData = FromBase64String(identityServerOptions.SigningCredential);
                    var cert    = new X509Certificate2(rawData, default(string), MachineKeySet);
                    builder.AddSigningCredential(cert);
                }

                if (!IsNullOrWhiteSpace(identityServerOptions.ValidationKey))
                {
                    var rawData = FromBase64String(identityServerOptions.ValidationKey);
                    var cert    = new X509Certificate2(rawData, default(string), MachineKeySet);
                    builder.AddValidationKey(cert);
                }

                builder.AddConfigurationStoreCache();
            }

            return(builder.Services);
        }