Example #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.RegisterInIdentityServerContainer();

            /* REJESTRACJA W KONTENERZE DI WSZYSTKICH SERWISÓW */
            services.Register(typeof(IScopeService), ServiceLifetime.Scoped);

            services.AddIdentity <User, IdentityRole>(options =>
            {
                options.Password.RequiredLength         = 6;
                options.Password.RequireLowercase       = true;
                options.Password.RequireUppercase       = true;
                options.Password.RequireDigit           = true;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequiredUniqueChars    = 0;
                options.Lockout.AllowedForNewUsers      = true;
                options.Lockout.MaxFailedAccessAttempts = 5;
                options.Lockout.DefaultLockoutTimeSpan  = TimeSpan.FromMinutes(5);
            })
            .AddUserManager <UserManager <User> >()
            .AddEntityFrameworkStores <IdentityContext>()
            .AddDefaultTokenProviders();

            var assembly = typeof(Initial).Assembly.GetName().Name;
            var masterConnectionString = Initial.ConnectionString(ConnectionStringType.Master).Result;

            services.AddIdentityServer(options =>
            {
                options.Events = new EventsOptions
                {
                    RaiseErrorEvents       = true,
                    RaiseInformationEvents = true,
                    RaiseFailureEvents     = true,
                    RaiseSuccessEvents     = true,
                };
                options.Authentication.CheckSessionCookieName  = "react-template.session";
                options.Authentication.CookieLifetime          = TimeSpan.FromMinutes(60);
                options.Authentication.CookieSlidingExpiration = true;
            })
            .AddConfigurationStore(option =>
                                   option.ConfigureDbContext = dbContextBuilder => dbContextBuilder.UseNpgsql(
                                       masterConnectionString,
                                       options => options.MigrationsAssembly(assembly)
                                       )
                                   )
            .AddOperationalStore(option =>
                                 option.ConfigureDbContext = dbContextBuilder => dbContextBuilder.UseNpgsql(
                                     masterConnectionString,
                                     options => options.MigrationsAssembly(assembly)
                                     )
                                 )
            .AddAspNetIdentity <User>()
            .AddDeveloperSigningCredential();

            services.AddControllersWithViews();
        }
Example #2
0
        public static T GenerateMasterContext <T>() where T : DbContext
        {
            var connectionString = Initial.ConnectionString(ConnectionStringType.Master).Result;

            var builder = new DbContextOptionsBuilder <T>();

            builder.UseNpgsql(connectionString);

            return((T)Activator.CreateInstance(typeof(T), builder.Options, typeof(T) == typeof(ConfigurationContext) ?
                                               (object)new ConfigurationStoreOptions
            {
                ConfigureDbContext = builder => builder.UseNpgsql(connectionString)
            } :
                                               new OperationalStoreOptions
            {
                ConfigureDbContext = builder => builder.UseNpgsql(connectionString)
            }
                                               ));
        }
Example #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            #region Rejestracja konfiguracji
            var identityServerSection = Configuration.GetSection(IdentityServerOptions.Name);
            var identityServerOptions = new IdentityServerOptions();

            services.Configure <IdentityServerOptions>(identityServerSection);
            identityServerSection.Bind(identityServerOptions);

            var queueSection = Configuration.GetSection(NotificationsOptions.Name);
            services.Configure <NotificationsOptions>(queueSection);
            #endregion
            #region Rejestracja generatora wydruków
            var pdfLibraryPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "libs", "libwkhtmltox", "libwkhtmltox");
            NativeLibrary.Load(pdfLibraryPath);

            services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
            #endregion

            services.RegisterInContainer();

            /* REJESTRACJA W KONTENERZE DI WSZYSTKICH SERWISÓW */
            services.Register(typeof(IScopeService), ServiceLifetime.Scoped);

            /* REJESTRACJA SERWISU POWIADOMIEÑ */
            services.AddSingleton <INotificationService, NotificationService>();

            services.AddControllers();

            /* NA PRODUKCJI PLIKI REACT BÊD¥ SERWOWANE Z TEGO KATALOGU */
            services.AddSpaStaticFiles(configuration => {
                configuration.RootPath = "ClientApp/build";
            });

            services.AddCors(options =>
            {
                options.AddDefaultPolicy(
                    builder =>
                {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod();
                });
            });

            services.AddAuthentication(options =>
            {
                options.DefaultScheme          = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
            {
                options.Authority    = identityServerOptions.Authority;
                options.ClientId     = identityServerOptions.Client;
                options.ClientSecret = identityServerOptions.Secret;
                options.Scope.Add(identityServerOptions.Scope);
                options.ResponseType = identityServerOptions.Response;
                options.SaveTokens   = true;
                options.Events       = new OpenIdConnectEvents
                {
                    OnRemoteFailure = context =>
                    {
                        context.Response.Redirect("/");
                        context.HandleResponse();

                        return(Task.CompletedTask);
                    }
                };
            })
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "React-Template API", Version = "v1"
                });
                c.EnableAnnotations();
            });

            services.AddHangfire(config =>
                                 config.UsePostgreSqlStorage(Initial.ConnectionString(ConnectionStringType.Master).GetAwaiter().GetResult())
                                 );
        }