[성현모] AuthApi 분리
This commit is contained in:
@ -1,25 +1,118 @@
|
||||
using AuthApi.Services;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.ComponentModel;
|
||||
using System.Text;
|
||||
using SystemX.Core.Services;
|
||||
using WebApi.Library.Config;
|
||||
|
||||
string configDir = @"../../Config";
|
||||
string configFileName = "WebApi.AuthApi.Config.json";
|
||||
|
||||
//raed log4net configs
|
||||
if (Log4net.IsConfigLoad == true)
|
||||
{
|
||||
Log4net.WriteLine("Log4net Init Success");
|
||||
Log4net.AutoRemoveLog();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Log4net Init Failed");
|
||||
return;
|
||||
}
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
//singleton
|
||||
builder.Services.AddSingleton<ConfigService<WebApiConfig>>();
|
||||
builder.Services.AddScoped<AuthService>();
|
||||
|
||||
//scoped
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
//config preload, auth set
|
||||
ConfigService<WebApiConfig> preloadConfig = new ConfigService<WebApiConfig>();
|
||||
if (preloadConfig.OpenConfig($@"{configDir}/{configFileName}") == true)
|
||||
{
|
||||
var config = preloadConfig.GetConfig();
|
||||
|
||||
//auth
|
||||
builder.Services
|
||||
.AddAuthentication(option =>
|
||||
{
|
||||
option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(options =>
|
||||
{
|
||||
options.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = true,
|
||||
ValidateAudience = true,
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
ValidIssuer = $"{config?.Auth?.issuer}",
|
||||
ValidAudience = $"{config?.Auth?.audience}",
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes($"{config?.Auth?.accessTokenSecret}"))
|
||||
};
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
Log4net.WriteLine("Config Preload Load Error.", LogType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
//read api config and set
|
||||
string serverUrl = string.Empty;
|
||||
var configService = app.Services.GetService<ConfigService<WebApiConfig>>();
|
||||
bool isIIS = false;
|
||||
|
||||
if (configService?.OpenConfig($@"{configDir}/{configFileName}") == true)
|
||||
{
|
||||
Log4net.WriteLine("WebApi Config Success.");
|
||||
var apiConfig = ConfigService<WebApiConfig>.Config;
|
||||
if (apiConfig != null)
|
||||
{
|
||||
serverUrl = $"{apiConfig?.Server?.Address}:{apiConfig?.Server?.Port}";
|
||||
isIIS = apiConfig!.Server.IIS;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Log4net.WriteLine("WebApi Config Error.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
Log4net.WriteLine($"IsDevelopment:{app.Environment.IsDevelopment()}");
|
||||
Log4net.WriteLine($"Swagger Url: {serverUrl}/swagger");
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
if (isIIS == true)
|
||||
{
|
||||
app.Run();
|
||||
}
|
||||
else
|
||||
{
|
||||
Log4net.WriteLine($"Operation Url: {serverUrl}");
|
||||
app.Run($"{serverUrl}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user