Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddControllers()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString;
            });

            services.AddDbContext <MainDbContext>(b =>
            {
                b.UseSqlite("Data Source=main.db;");
            });

            services.AddIdentity <ApplicationUser, IdentityRole>(c =>
            {
            })
            .AddEntityFrameworkStores <MainDbContext>();


            services.AddScoped <IAuthenticationHandler, BasicAuthenticationHandler>();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(c =>
            {
                c.TokenValidationParameters = new TokenValidationParameters
                {
                    ClockSkew = TimeSpan.FromMinutes(5),
                    // TODO: Create signing key from certificate
                    IssuerSigningKey      = SecurityUtils.CreateSecurityKey("THIS_IS_A_SECRET_THAT_I_AM_MAKING_LONGER"),
                    RequireSignedTokens   = true,
                    RequireExpirationTime = true,
                    ValidateLifetime      = true,
                    // TODO: Do stuff with Audience and Issuer
                };
            })
            .AddCookie(c =>
            {
                c.LoginPath  = "/api/AccountSvc/SignIn";
                c.LogoutPath = "/api/AccountSvc/SignOut";
            })
            .AddScheme <AuthenticationSchemeOptions, BasicAuthenticationHandler>("Basic", c => { });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Home Inventory", Version = "v1"
                });

                var jwtSecurityScheme = new OpenApiSecurityScheme
                {
                    Type      = SecuritySchemeType.Http,
                    Scheme    = "bearer",
                    In        = ParameterLocation.Header,
                    Reference = new OpenApiReference {
                        Id = "Bearer", Type = ReferenceType.SecurityScheme
                    }
                };

                var basicSecurityScheme = new OpenApiSecurityScheme
                {
                    Type      = SecuritySchemeType.Http,
                    Scheme    = "basic",
                    In        = ParameterLocation.Header,
                    Reference = new OpenApiReference {
                        Id = "Basic", Type = ReferenceType.SecurityScheme
                    }
                };

                c.AddSecurityDefinition(basicSecurityScheme.Reference.Id, basicSecurityScheme);
                c.AddSecurityDefinition(jwtSecurityScheme.Reference.Id, jwtSecurityScheme);

                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    { basicSecurityScheme, Array.Empty <string>() },
                    { jwtSecurityScheme, Array.Empty <string>() }
                });
            });
        }