Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            var typeContainer = TypeContainer.Get <ITypeContainer>();

            SQLite.SqliteStartup.Register(typeContainer);
            RegisterController(typeContainer);
            typeContainer.Register <IControllerActivator, CustomControllerActivator>(InstanceBehaviour.Singleton);

            services.AddControllersWithViews();
            services.AddSingleton(typeContainer.Get <IControllerActivator>());

            typeContainer.Register <JobService>(InstanceBehaviour.Singleton);
            typeContainer.Register <GameService>(InstanceBehaviour.Singleton);
            typeContainer.Register(typeof(GameProvider), typeof(GameProvider), GameProvider.Create());

            var sessionService = new UserSessionService($"{nameof(Haraka)}.{nameof(Server)}.JWT");

            typeContainer.Register <IUserSessionService>(sessionService);

            sessionService.LoadOrCreateKey();
            typeContainer.Get <IDbProvider>().RegisterDatabase("main.db", typeContainer);

            services
            .AddAuthentication(auth =>
            {
                auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                auth.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(jwt =>
            {
                jwt.SaveToken = true;
                jwt.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidateIssuerSigningKey = true,
                    ValidateAudience         = false,
                    IssuerSigningKey         = sessionService.Key,
                    ValidIssuer = sessionService.Issuer
                };
                jwt.SecurityTokenValidators.Clear();
                jwt.SecurityTokenValidators.Add(sessionService);

#if DEBUG
                jwt.RequireHttpsMetadata = false;
#endif
            });
        }