public void ConfigureServices(IServiceCollection services)
{
services
.AddControllers()
.AddNewtonsoftJson();
services.AddInfrastructure();
services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});
services.AddDbContext<AccessControlContext>
((sp, options) =>
{
options.UseSqlServer(
sp.GetRequiredService<IConfiguration>().GetConnectionString("appservice-db"));
options.EnableSensitiveDataLogging();
})
.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<AccessControlContext>()
.AddDefaultTokenProviders();
services.AddCors(options =>
{
options.AddPolicy("AllowAnyOrigin",
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
services.AddAuthorization(options =>
{
options.AddPolicy(JwtBearerDefaults.AuthenticationScheme, policy =>
{
policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme);
policy.RequireClaim(ClaimTypes.NameIdentifier);
});
options.AddPolicy("AdministratorsOnly", policy => policy.RequireClaim("Administrator"));
});
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
if (!string.IsNullOrEmpty(accessToken) &&
(context.HttpContext.WebSockets.IsWebSocketRequest || context.Request.Headers["Accept"] == "text/event-stream"))
{
context.Token = context.Request.Query["access_token"];
}
return Task.CompletedTask;
}
};
});
services.AddOpenApiDocument();
/*
services.AddSingleton(sp => NotificationHubClient.CreateClientFromConnectionString(Configuration["Notifications:ConnectionString"], Configuration["Notifications:Path"]));
services.AddSingleton(sp => ServiceClient.CreateFromConnectionString(Configuration["Hub:ConnectionString"]));
services.AddSingleton(sp => EventHubClient.CreateFromConnectionString(Configuration["Events:ConnectionString"]));
*/
services.AddMassTransit(x =>
{
x.SetKebabCaseEndpointNameFormatter();
x.AddConsumers(typeof(AppService.Application.Alarm.AlarmConsumer).Assembly);
x.UsingRabbitMq((context, cfg) =>
{
cfg.ConfigureEndpoints(context);
});
})
.AddMassTransitHostedService()
.AddGenericRequestClient();
services.AddSingleton<DeviceController>();
services.AddTransient<IJwtTokenService, JwtTokenService>();
services.AddSingleton<IAccessLogNotifier, AccessLogNotifier>();
services.AddSingleton<IAccessLogger, AccessLogger>();
services.AddSignalR();
services.AddMediatR(typeof(AuthCommand));
//services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidatorBehavior<,>));
//services.AddValidatorsFromAssemblyContaining<LoginCommandValidator>();
}