- 配置AuthService使用OpenIddict reference tokens - 添加fengling-api客户端用于introspection验证 - 配置Console API通过OpenIddict验证reference tokens - 实现Tenant/Users/Roles/OAuthClients CRUD API - 添加GatewayController服务注册API - 重构Repository和Service层支持多租户 BREAKING CHANGE: API认证现在使用OpenIddict reference tokens
106 lines
2.9 KiB
C#
106 lines
2.9 KiB
C#
using System.Reflection;
|
|
using Fengling.Console.Repositories;
|
|
using Fengling.Console.Services;
|
|
using OpenIddict.Abstractions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
using Fengling.Console.Datas;
|
|
using Fengling.Console.Models.Entities;
|
|
using OpenIddict.Validation.AspNetCore;
|
|
using YarpGateway.Data;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|
{
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"));
|
|
});
|
|
|
|
builder.Services.AddDbContext<GatewayDbContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("GatewayConnection")));
|
|
|
|
builder.Services.AddIdentity<ApplicationUser, ApplicationRole>()
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
builder.Services.AddHttpClient();
|
|
builder.Services.AddScoped<IOAuthClientService, OAuthClientService>();
|
|
builder.Services.AddScoped<IUserRepository, UserRepository>();
|
|
builder.Services.AddScoped<ITenantRepository, TenantRepository>();
|
|
builder.Services.AddScoped<IRoleRepository, RoleRepository>();
|
|
|
|
builder.Services.AddScoped<IUserService, UserService>();
|
|
builder.Services.AddScoped<ITenantService, TenantService>();
|
|
builder.Services.AddScoped<IRoleService, RoleService>();
|
|
|
|
builder.Services.AddOpenIddict()
|
|
.AddCore(options =>
|
|
{
|
|
options.UseEntityFrameworkCore().UseDbContext<ApplicationDbContext>();
|
|
})
|
|
.AddValidation(options =>
|
|
{
|
|
options.SetIssuer("http://localhost:5132/");
|
|
|
|
options.UseIntrospection()
|
|
.SetClientId("fengling-api")
|
|
.SetClientSecret("fengling-api-secret");
|
|
|
|
options.UseSystemNetHttp();
|
|
|
|
options.UseAspNetCore();
|
|
});
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultScheme = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme;
|
|
});
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("AllowAll", policy =>
|
|
{
|
|
policy.AllowAnyOrigin()
|
|
.AllowAnyMethod()
|
|
.AllowAnyHeader();
|
|
});
|
|
});
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
c.SwaggerDoc("v1", new() { Title = "Fengling.Console API", Version = "v1" });
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
if (File.Exists(xmlPath))
|
|
{
|
|
c.IncludeXmlComments(xmlPath);
|
|
}
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI(c =>
|
|
{
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Fengling.Console API V1");
|
|
});
|
|
|
|
app.UseCors("AllowAll");
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|