// This method gets called by a runtime. // Use this method to add services to the container public void ConfigureServices(IServiceCollection services) { services.AddCaching(); services.AddSession(o => { o.IdleTimeout = TimeSpan.FromSeconds(10); }); services.AddMvc(options => { options.Filters.Add(new GlobalExceptionFilter()); }); services.Configure <AppSettings>(Configuration.GetSection("AppSettings")); var appSettings = Configuration.Get <AppSettings>(); key = RSAKeyUtils.GetKey(); tokenOptions = new TokenAuthOptions("ExampleAudience", "ExampleIssuer", key); services.AddInstance <TokenAuthOptions>(tokenOptions); services.AddAuthorization(auth => { auth.AddPolicy(TokenAuthOptions.Scheme, new AuthorizationPolicyBuilder() .AddAuthenticationSchemes(TokenAuthOptions.Scheme) .RequireAuthenticatedUser() .AddRequirements(new TokenAuthRequirement()) .Build()); }); DependencyInstaller.InjectDependencies(services, this.Configuration); _logger.LogInformation("Configuring Services"); }
private void ConfigureCommonObjects(IServiceCollection services) { AnraConfiguration.Configure(services, Configuration); //services.AddSingleton(new SendNotification(services.BuildServiceProvider().GetRequiredService<AnraConfiguration>())); services.AddSingleton(new TokenProviderOptions { Audience = "AnraUsers", Issuer = "AnraTechnologies", SigningCredentials = new SigningCredentials(new RsaSecurityKey(RSAKeyUtils.GetKey()), SecurityAlgorithms.RsaSha256Signature), Expiration = TimeSpan.FromMinutes(Configuration.GetSection("LoginSettings").GetValue <double>("Expiration")) }); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure <FormOptions>(x => { x.ValueLengthLimit = int.MaxValue; x.BufferBodyLengthLimit = Int64.MaxValue; x.MultipartBodyLengthLimit = Int64.MaxValue; }); var sqlConnectionString = Configuration.GetConnectionString("PostgreSqlProviderPath"); services.AddDbContext <MantiDbContext>(options => options.UseNpgsql( sqlConnectionString, b => b.MigrationsAssembly(nameof(MantiScanServices)) ) ); ConfigureCommonObjects(services); services.AddCors(); services.AddIdentity <User, IdentityRole>(config => { config.User.RequireUniqueEmail = true; config.Password.RequiredLength = 6; config.Password.RequireUppercase = false; config.Password.RequireNonAlphanumeric = false; }) .AddEntityFrameworkStores <MantiDbContext>() .AddDefaultTokenProviders(); services.AddAuthentication(o => { o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; o.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.Audience = "AnraUsers"; options.ClaimsIssuer = "AnraTechnologies"; options.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = "AnraTechnologies", IssuerSigningKey = new RsaSecurityKey(RSAKeyUtils.GetKey()) }; }); services.AddLogging(loggingBuilder => { loggingBuilder.AddSerilog(); }); services.AddMvc() .AddJsonOptions(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); services.AddAuthorization(options => { options.AddPolicy("Bearer", authBuilder => { authBuilder.RequireRole("Administrators"); }); }); AddCustomDependencies(services); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "MantiScan API", Version = "MantiScan Services v1.0.0" }); c.DescribeAllEnumsAsStrings(); var basePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, Assembly.GetExecutingAssembly().GetName().Name + ".xml"); c.IncludeXmlComments(basePath); }); }