230 lines
8.2 KiB
C#
230 lines
8.2 KiB
C#
using Manager.Framework.Business;
|
|
using Manager.Framework.Models;
|
|
using Manager.Interfaces.Models;
|
|
using Manager.Services;
|
|
using ManagerService.Extensions;
|
|
using ManagerService.Service;
|
|
using ManagerService.Service.Services;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Diagnostics;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
using Microsoft.AspNetCore.HttpsPolicy;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Mqtt.Client.AspNetCore.Settings;
|
|
using MyCore.Service.Extensions;
|
|
using NSwag;
|
|
using NSwag.Generation.AspNetCore;
|
|
using NSwag.Generation.Processors.Security;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json.Serialization;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ManagerService
|
|
{
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
|
|
MapConfiguration();
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
private void MapConfiguration()
|
|
{
|
|
MapBrokerHostSettings();
|
|
MapClientSettings();
|
|
}
|
|
|
|
private void MapBrokerHostSettings()
|
|
{
|
|
BrokerHostSettings brokerHostSettings = new BrokerHostSettings();
|
|
Configuration.GetSection(nameof(BrokerHostSettings)).Bind(brokerHostSettings);
|
|
AppSettingsProvider.BrokerHostSettings = brokerHostSettings;
|
|
}
|
|
|
|
private void MapClientSettings()
|
|
{
|
|
ClientSettings clientSettings = new ClientSettings();
|
|
Configuration.GetSection(nameof(ClientSettings)).Bind(clientSettings);
|
|
AppSettingsProvider.ClientSettings = clientSettings;
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
// Swagger
|
|
services.AddControllers()
|
|
.AddJsonOptions(options =>
|
|
{
|
|
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
|
});
|
|
|
|
services.AddOpenApiDocument(config =>
|
|
{
|
|
ConfigureSwagger(config);
|
|
});
|
|
|
|
services.AddCors(o => o.AddPolicy("AllowAll", builder =>
|
|
{
|
|
builder.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader();
|
|
}));
|
|
|
|
services.Configure<FormOptions>(o => {
|
|
o.ValueLengthLimit = int.MaxValue;
|
|
o.MultipartBodyLengthLimit = int.MaxValue;
|
|
o.MemoryBufferThreshold = int.MaxValue;
|
|
});
|
|
|
|
// Authentication
|
|
|
|
var tokensConfiguration = Configuration.GetSection("Tokens");
|
|
var tokenSettings = tokensConfiguration.Get<TokensSettings>();
|
|
|
|
services.Configure<TokensSettings>(tokensConfiguration);
|
|
|
|
foreach (var policy in Security.PoliciesConfiguration)
|
|
services.AddAuthorization(options =>
|
|
{
|
|
options.AddPolicy(policy.Name, policyAdmin =>
|
|
{
|
|
foreach (var claim in policy.Claims)
|
|
policyAdmin.RequireClaim(Security.ClaimTypes.Permission, claim);
|
|
});
|
|
});
|
|
|
|
services
|
|
.AddAuthentication(x =>
|
|
{
|
|
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(x =>
|
|
{
|
|
x.RequireHttpsMetadata = false;
|
|
x.SaveToken = true;
|
|
x.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenSettings.Secret)),
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
RequireExpirationTime = false,
|
|
ValidateLifetime = true
|
|
};
|
|
});
|
|
|
|
services.AddMqttClientHostedService();
|
|
services.AddScoped(typeof(ProfileLogic));
|
|
services.AddScoped<TokensService>();
|
|
services.AddScoped<UserDatabaseService>();
|
|
services.AddScoped<SectionDatabaseService>();
|
|
services.AddScoped<ConfigurationDatabaseService>();
|
|
services.AddScoped<ResourceDatabaseService>();
|
|
services.AddScoped<LanguageInit>();
|
|
services.AddScoped<DeviceDatabaseService>();
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
/*app.UseCors(
|
|
options => options.WithOrigins("http://localhost:60109").AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials()
|
|
);*/
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
//app.UseDeveloperExceptionPage();
|
|
app.UseExceptionHandler(HandleError);
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseRouting();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.UseCors("AllowAll");
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
|
|
app.UseOpenApi();
|
|
app.UseSwaggerUi3(configure =>
|
|
{
|
|
configure.OperationsSorter = "alpha";
|
|
configure.TagsSorter = "alpha";
|
|
});
|
|
}
|
|
|
|
private void ConfigureSwagger(AspNetCoreOpenApiDocumentGeneratorSettings config)
|
|
{
|
|
config.GenerateEnumMappingDescription = true;
|
|
config.AddSecurity("bearer", Enumerable.Empty<string>(), new OpenApiSecurityScheme
|
|
{
|
|
Type = OpenApiSecuritySchemeType.OAuth2,
|
|
Description = "Manager Authentication",
|
|
Flow = OpenApiOAuth2Flow.Password,
|
|
Flows = new OpenApiOAuthFlows()
|
|
{
|
|
|
|
Password = new OpenApiOAuthFlow()
|
|
{
|
|
Scopes = new Dictionary<string, string>
|
|
{
|
|
{ Security.Scope, "Manager WebAPI" }
|
|
},
|
|
TokenUrl = "/api/authentication/Token",
|
|
AuthorizationUrl = "/authentication/Token",
|
|
}
|
|
}
|
|
});
|
|
config.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer"));
|
|
|
|
config.PostProcess = document =>
|
|
{
|
|
document.Info.Title = "Manager Service";
|
|
document.Info.Description = "API Manager Service";
|
|
document.Info.Version = "Version Alpha";
|
|
};
|
|
}
|
|
|
|
|
|
private void HandleError(IApplicationBuilder error)
|
|
{
|
|
error.Run(async context =>
|
|
{
|
|
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
|
|
var exception = exceptionHandlerPathFeature?.Error as RequestException;
|
|
|
|
if (exception != null)
|
|
{
|
|
var json = exception.GetJson();
|
|
context.Response.ContentType = "application/json";
|
|
context.Response.StatusCode = exception.StatusCode;
|
|
await context.Response.WriteAsync(json);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|