Ejemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers()
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            services.AddSession();

            #region IISOptions
            services.Configure <IISOptions>(options =>
            {
                options.AutomaticAuthentication   = false;
                options.AuthenticationDisplayName = "Windows";
            });
            #endregion

            #region Identity Server

            var builder = services.AddIdentityServer(options =>
            {
                // options.PublicOrigin = "https://localhost:6001";
                // options.IssuerUri = "https://localhost:6001";
                options.Events.RaiseErrorEvents       = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents     = true;
                options.Events.RaiseSuccessEvents     = true;
            });

            // Signing credential
            builder.AddDeveloperSigningCredential();

            // Set in-memory, code config
            builder.AddInMemoryIdentityResources(InMemoryInitConfig.GetIdentityResources());
            builder.AddInMemoryApiResources(InMemoryInitConfig.GetApiResources());
            builder.AddInMemoryClients(InMemoryInitConfig.GetClients());
            builder.AddLdapUsers <OpenLdapAppUser>(this.Configuration.GetSection("LdapServer"), UserStore.InMemory); // OpenLDAP
                                                                                                                     // builder.AddLdapUsers<ActiveDirectoryAppUser>(this.Configuration.GetSection("LdapServer"), UserStore.InMemory); // ActiveDirectory

            builder.AddProfileService <ProfileService>();

            #endregion

            #region  Inject Cache service
            services.AddMemoryCache();
            services.AddCacheServices();
            #endregion

            #region Custom sinks
            services.AddScoped <IEventSink, UserProfileCacheSink>();
            #endregion
        }
        /// <summary>
        /// Configure services
        /// </summary>
        /// <param name="services">Service collection</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews()
            .AddRazorOptions(
                options => {
                //{2} is area, {1} is controller,{0} is the action
                options.ViewLocationFormats.Add("/Areas/{1}/Views/{0}.cshtml");
            }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            services.AddSession();

            #region Inject AppSetting configuration

            services.Configure <AppSettings>(this.configuration);

            // Set static AppSettingProvider
            var globalOptions = new GlobalOptions();
            configuration.GetSection("Global").Bind(globalOptions);
            AppSettingProvider.Global = globalOptions;
            #endregion

            #region OpenAPI specification (Swagger)
            services.AddOpenApiSpec <CustomSwaggerConfig>();
            #endregion

            #region IISOptions
            services.Configure <IISOptions>(options =>
            {
                options.AutomaticAuthentication   = false;
                options.AuthenticationDisplayName = "Windows";
            });
            #endregion

            #region Identity Server

            var builder = services.AddIdentityServer(options =>
            {
                // options.PublicOrigin = "https://localhost:6001";
                // options.IssuerUri = "https://localhost:6001";
                options.Events.RaiseErrorEvents       = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents     = true;
                options.Events.RaiseSuccessEvents     = true;

                options.Discovery.ResponseCacheInterval = 60;
            });

            // Signing credential
            if (this.env.IsDevelopment())
            {
                builder.AddDeveloperSigningCredential();
            }
            else
            {
                // 1. Store in file (Support renew manually)
                // builder.AddSigningCredentialsByFile(this.appSettings);

                // 2. Store in Redis (Support renew automatically)
                builder.AddSigningCredentialByRedis(this.appSettings);

                // 3. Use cert
                // builder.AddSigningCredentialByCert(this.appSettings, isFromWindowsCertStore: true);
            }

            // Set in-memory, code config
            builder.AddInMemoryIdentityResources(InMemoryInitConfig.GetIdentityResources());
            builder.AddInMemoryApiResources(InMemoryInitConfig.GetApiResources());
            builder.AddInMemoryClients(InMemoryInitConfig.GetClients());
            builder.AddLdapUsers <OpenLdapAppUser>(this.configuration.GetSection("LdapServer"), UserStore.InMemory); // OpenLDAP
            //builder.AddLdapUsers<ActiveDirectoryAppUser>(this.configuration.GetSection("LdapServer"), UserStore.InMemory); // ActiveDirectory

            builder.AddProfileService <ProfileService>();

            #endregion

            #region  Inject Cache service
            services.AddMemoryCache();
            services.AddCacheServices();
            #endregion

            #region Custom sinks
            services.AddScoped <IEventSink, UserProfileCacheSink>();
            #endregion

            #region Custom services
            services.AddSingleton <LdapUserManager>();
            #endregion
        }
Ejemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(typeof(ITelemetryChannel),
                                  new ServerTelemetryChannel()
            {
                StorageFolder = "/logging"
            });
            Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration).CreateLogger();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            var identityConfiguration =
                Configuration.GetSection("IdentityConfiguration").Get <IdentityConfiguration>();

            services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
            .AddIdentityServerAuthentication(options =>
            {
                options.RequireHttpsMetadata = false;
                // The API resource scope issued in authorization server
                options.ApiName = identityConfiguration.ApiName;
                // URL of my authorization server
                options.Authority = identityConfiguration.Authority;
            });

            // Making JWT authentication scheme the default
            services.AddAuthorization(options =>
            {
                options.DefaultPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
                                        .RequireAuthenticatedUser()
                                        .Build();
            });

            var hasLDAPConfiguration = Configuration.GetSection("Authentication:LDAPConnectionOptions").GetChildren().Any();

            if (hasLDAPConfiguration)
            {
                _logger.LogInformation("Using LDAP based authentication");
                services.AddIdentityServer(options =>
                {
                    options.Events.RaiseErrorEvents       = true;
                    options.Events.RaiseFailureEvents     = true;
                    options.Events.RaiseInformationEvents = true;
                    options.Events.RaiseSuccessEvents     = true;;
                })
                .AddDeveloperSigningCredential()
                .AddInMemoryIdentityResources(InMemoryInitConfig.GetIdentityResources())
                .AddInMemoryApiResources(InMemoryInitConfig.GetApiResources())
                .AddInMemoryClients(InMemoryInitConfig.GetClients(identityConfiguration.AllowedOrigins))
                .AddProfileService <FullNameProfileService>()
                .AddResourceOwnerValidator <LDAPResourceOwnerPasswordValidator>();
            }
            else
            {
                _logger.LogInformation("Using file based authentication");
                services.AddIdentityServer(options =>
                {
                    options.Events.RaiseErrorEvents       = true;
                    options.Events.RaiseFailureEvents     = true;
                    options.Events.RaiseInformationEvents = true;
                    options.Events.RaiseSuccessEvents     = true;
                    ;
                })
                .AddDeveloperSigningCredential()
                .AddInMemoryIdentityResources(InMemoryInitConfig.GetIdentityResources())
                .AddInMemoryApiResources(InMemoryInitConfig.GetApiResources())
                .AddInMemoryClients(InMemoryInitConfig.GetClients(identityConfiguration.AllowedOrigins))
                .AddProfileService <FullNameProfileService>()
                .AddResourceOwnerValidator <FileBasedResourceOwnerPasswordValidator>();
            }



            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                                  builder => builder.AllowAnyOrigin()
                                  .AllowAnyMethod()
                                  .AllowAnyHeader());
            });

            services.AddLogging();
            ;
            var doorConfiguration = Configuration.GetSection("MQTTDoorConfiguration").Get <DoorConfiguration>();

            services.AddScoped <ITotpGenerator, TotpGenerator>();
            services.AddScoped <ITotpSetupGenerator, TotpSetupGenerator>();
            services.AddScoped <ITotpValidator, TotpValidator>();
            services.AddScoped <IBrixelOpenDoorClient>(x =>
                                                       new BrixelOpenDoorClient(
                                                           doorConfiguration.ClientId,
                                                           doorConfiguration.Server,
                                                           doorConfiguration.Topic,
                                                           doorConfiguration.Port,
                                                           doorConfiguration.UseSSL,
                                                           doorConfiguration.Username,
                                                           doorConfiguration.Password
                                                           ));
            services.AddScoped <IDoorRequestService, DoorRequestService>();
            services.Configure <AccountKeyConfiguration>(Configuration.GetSection("AccountKeyConfiguration"));
            services.AddScoped <IAccountKeyService, AccountKeyService>();
        }
        /// <summary>
        /// Configure services
        /// </summary>
        /// <param name="services">Service collection</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers()
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            services.AddSession();

            #region Inject AppSetting configuration

            services.Configure <AppSettings>(this.configuration);

            // Set static AppSettingProvider
            var globalOptions = new GlobalOptions();
            configuration.GetSection("Global").Bind(globalOptions);
            AppSettingProvider.Global = globalOptions;
            #endregion

            #region API Versioning

            services.AddApiVersioning(opt =>
            {
                opt.ReportApiVersions = true;                                              // List supported versons on Http header
                opt.DefaultApiVersion = new ApiVersion(1, 0);                              // Set the default version
                opt.AssumeDefaultVersionWhenUnspecified = true;                            // Use the api of default version
                opt.ApiVersionSelector = new CurrentImplementationApiVersionSelector(opt); // Use the api of latest release number
            });
            #endregion

            #region API Document (Swagger)

            services.AddVersionedApiExplorer(options => options.GroupNameFormat = "'v'VVV");
            services.AddTransient <IConfigureOptions <SwaggerGenOptions>, SwaggerConfig>();
            services.AddSwaggerGen(c =>
            {
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = System.IO.Path.Combine(System.AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);

                // Set the custom operation filter
                c.OperationFilter <DeprecatedOperationFilter>();
            });
            #endregion

            #region IISOptions
            services.Configure <IISOptions>(options =>
            {
                options.AutomaticAuthentication   = false;
                options.AuthenticationDisplayName = "Windows";
            });
            #endregion

            #region Identity Server

            var builder = services.AddIdentityServer(options =>
            {
                // options.PublicOrigin = "https://localhost:6001";
                // options.IssuerUri = "https://localhost:6001";
                options.Events.RaiseErrorEvents       = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents     = true;
                options.Events.RaiseSuccessEvents     = true;

                options.Discovery.ResponseCacheInterval = 60;
            });

            // Signing credential
            if (this.env.IsDevelopment())
            {
                builder.AddDeveloperSigningCredential();
            }
            else
            {
                // 1. Store in file (Support renew manually)
                // builder.AddSigningCredentialsByFile(this.appSettings);

                // 2. Store in Redis (Support renew automatically)
                builder.AddSigningCredentialByRedis(this.appSettings);

                // 3. Use cert
                // builder.AddSigningCredentialByCert(this.appSettings, isFromWindowsCertStore: true);
            }

            // Set in-memory, code config
            builder.AddInMemoryIdentityResources(InMemoryInitConfig.GetIdentityResources());
            builder.AddInMemoryApiResources(InMemoryInitConfig.GetApiResources());
            builder.AddInMemoryClients(InMemoryInitConfig.GetClients());
            builder.AddLdapUsers <OpenLdapAppUser>(this.configuration.GetSection("LdapServer"), UserStore.InMemory); // OpenLDAP
            //builder.AddLdapUsers<ActiveDirectoryAppUser>(this.configuration.GetSection("LdapServer"), UserStore.InMemory); // ActiveDirectory

            builder.AddProfileService <ProfileService>();

            #endregion

            #region  Inject Cache service
            services.AddMemoryCache();
            services.AddCacheServices();
            #endregion

            #region Custom sinks
            services.AddScoped <IEventSink, UserProfileCacheSink>();
            #endregion

            #region Custom services
            services.AddSingleton <LdapUserManager>();
            #endregion
        }