324 lines
12 KiB
C#
324 lines
12 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.AspNetCore.Extensions.DependencyInjection;
|
|
using OpenIddict.Validation.AspNetCore;
|
|
using OpenIddict.Server.AspNetCore;
|
|
using Hua.Abp.Demo.EntityFrameworkCore;
|
|
using Hua.Abp.Demo.MultiTenancy;
|
|
using Hua.Abp.Demo.HealthChecks;
|
|
using Microsoft.OpenApi.Models;
|
|
using Volo.Abp;
|
|
using Volo.Abp.Studio;
|
|
using Volo.Abp.Account;
|
|
using Volo.Abp.Account.Web;
|
|
using Volo.Abp.AspNetCore.MultiTenancy;
|
|
using Volo.Abp.AspNetCore.Mvc;
|
|
using Volo.Abp.Autofac;
|
|
using Volo.Abp.Localization;
|
|
using Volo.Abp.Modularity;
|
|
using Volo.Abp.UI.Navigation.Urls;
|
|
using Volo.Abp.VirtualFileSystem;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.LeptonXLite;
|
|
using Volo.Abp.AspNetCore.Mvc.UI.Theme.LeptonXLite.Bundling;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Volo.Abp.AspNetCore.Serilog;
|
|
using Volo.Abp.Identity;
|
|
using Volo.Abp.OpenIddict;
|
|
using Volo.Abp.Swashbuckle;
|
|
using Volo.Abp.Studio.Client.AspNetCore;
|
|
using Volo.Abp.Security.Claims;
|
|
|
|
namespace Hua.Abp.Demo;
|
|
|
|
[DependsOn(
|
|
typeof(DemoHttpApiModule),
|
|
typeof(AbpStudioClientAspNetCoreModule),
|
|
typeof(AbpAspNetCoreMvcUiLeptonXLiteThemeModule),
|
|
typeof(AbpAutofacModule),
|
|
typeof(AbpAspNetCoreMultiTenancyModule),
|
|
typeof(DemoApplicationModule),
|
|
typeof(DemoEntityFrameworkCoreModule),
|
|
typeof(AbpAccountWebOpenIddictModule),
|
|
typeof(AbpSwashbuckleModule),
|
|
typeof(AbpAspNetCoreSerilogModule)
|
|
)]
|
|
public class DemoHttpApiHostModule : AbpModule
|
|
{
|
|
public override void PreConfigureServices(ServiceConfigurationContext context)
|
|
{
|
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
|
var configuration = context.Services.GetConfiguration();
|
|
|
|
PreConfigure<OpenIddictBuilder>(builder =>
|
|
{
|
|
builder.AddValidation(options =>
|
|
{
|
|
options.AddAudiences("Demo");
|
|
options.UseLocalServer();
|
|
options.UseAspNetCore();
|
|
});
|
|
});
|
|
|
|
if (!hostingEnvironment.IsDevelopment())
|
|
{
|
|
PreConfigure<AbpOpenIddictAspNetCoreOptions>(options =>
|
|
{
|
|
options.AddDevelopmentEncryptionAndSigningCertificate = false;
|
|
});
|
|
|
|
PreConfigure<OpenIddictServerBuilder>(serverBuilder =>
|
|
{
|
|
serverBuilder.AddProductionEncryptionAndSigningCertificate("openiddict.pfx", configuration["AuthServer:CertificatePassPhrase"]!);
|
|
serverBuilder.SetIssuer(new Uri(configuration["AuthServer:Authority"]!));
|
|
});
|
|
}
|
|
else
|
|
{
|
|
// DEV ONLY: Allow Gitea to connect via HTTP without forcing HTTPS metadata
|
|
PreConfigure<OpenIddictServerBuilder>(serverBuilder =>
|
|
{
|
|
serverBuilder.UseAspNetCore().DisableTransportSecurityRequirement();
|
|
});
|
|
}
|
|
}
|
|
|
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
|
{
|
|
var configuration = context.Services.GetConfiguration();
|
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
|
|
|
if (!configuration.GetValue<bool>("App:DisablePII"))
|
|
{
|
|
Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
|
|
Microsoft.IdentityModel.Logging.IdentityModelEventSource.LogCompleteSecurityArtifact = true;
|
|
}
|
|
|
|
if (!configuration.GetValue<bool>("AuthServer:RequireHttpsMetadata"))
|
|
{
|
|
Configure<OpenIddictServerAspNetCoreOptions>(options =>
|
|
{
|
|
options.DisableTransportSecurityRequirement = true;
|
|
});
|
|
|
|
Configure<ForwardedHeadersOptions>(options =>
|
|
{
|
|
options.ForwardedHeaders = ForwardedHeaders.XForwardedProto;
|
|
});
|
|
}
|
|
|
|
ConfigureAuthentication(context);
|
|
ConfigureUrls(configuration);
|
|
ConfigureBundles();
|
|
ConfigureConventionalControllers();
|
|
ConfigureHealthChecks(context);
|
|
ConfigureSwagger(context, configuration);
|
|
ConfigureVirtualFileSystem(context);
|
|
ConfigureCors(context, configuration);
|
|
}
|
|
|
|
private void ConfigureAuthentication(ServiceConfigurationContext context)
|
|
{
|
|
context.Services.ForwardIdentityAuthenticationForBearer(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme);
|
|
|
|
var configuration = context.Services.GetConfiguration();
|
|
context.Services.AddAuthentication()
|
|
.AddOpenIdConnect("WeGit", "Login with WeGit", options =>
|
|
{
|
|
options.Authority = "https://git.we965.cn";
|
|
options.ClientId = configuration["Authentication:WeGit:ClientId"]!;
|
|
options.ClientSecret = configuration["Authentication:WeGit:ClientSecret"]!;
|
|
options.ResponseType = "code";
|
|
|
|
// Gitea specific scopes or defaults
|
|
options.Scope.Add("openid");
|
|
options.Scope.Add("profile");
|
|
options.Scope.Add("email");
|
|
|
|
// Ensure HTTPS
|
|
options.RequireHttpsMetadata = true;
|
|
options.SaveTokens = true;
|
|
|
|
// Handle events if needed, e.g. mapping claims
|
|
options.Events.OnTokenValidated = async context =>
|
|
{
|
|
// You might need to map specific claims here if Gitea returns non-standard ones
|
|
await Task.CompletedTask;
|
|
};
|
|
|
|
// Bypass SSL certificate validation (DEV ONLY)
|
|
options.BackchannelHttpHandler = new System.Net.Http.HttpClientHandler
|
|
{
|
|
ServerCertificateCustomValidationCallback = System.Net.Http.HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
|
|
};
|
|
});
|
|
|
|
context.Services.Configure<AbpClaimsPrincipalFactoryOptions>(options =>
|
|
{
|
|
options.IsDynamicClaimsEnabled = true;
|
|
});
|
|
}
|
|
|
|
private void ConfigureUrls(IConfiguration configuration)
|
|
{
|
|
Configure<AppUrlOptions>(options =>
|
|
{
|
|
options.Applications["MVC"].RootUrl = configuration["App:SelfUrl"];
|
|
options.Applications["Angular"].RootUrl = configuration["App:AngularUrl"];
|
|
options.Applications["Angular"].Urls[AccountUrlNames.PasswordReset] = "account/reset-password";
|
|
options.RedirectAllowedUrls.AddRange(configuration["App:RedirectAllowedUrls"]?.Split(',') ?? Array.Empty<string>());
|
|
});
|
|
}
|
|
|
|
private void ConfigureBundles()
|
|
{
|
|
Configure<AbpBundlingOptions>(options =>
|
|
{
|
|
options.StyleBundles.Configure(
|
|
LeptonXLiteThemeBundles.Styles.Global,
|
|
bundle =>
|
|
{
|
|
bundle.AddFiles("/global-styles.css");
|
|
}
|
|
);
|
|
|
|
options.ScriptBundles.Configure(
|
|
LeptonXLiteThemeBundles.Scripts.Global,
|
|
bundle =>
|
|
{
|
|
bundle.AddFiles("/global-scripts.js");
|
|
}
|
|
);
|
|
});
|
|
}
|
|
|
|
|
|
private void ConfigureVirtualFileSystem(ServiceConfigurationContext context)
|
|
{
|
|
var hostingEnvironment = context.Services.GetHostingEnvironment();
|
|
|
|
if (hostingEnvironment.IsDevelopment())
|
|
{
|
|
Configure<AbpVirtualFileSystemOptions>(options =>
|
|
{
|
|
options.FileSets.ReplaceEmbeddedByPhysical<DemoDomainSharedModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Hua.Abp.Demo.Domain.Shared"));
|
|
options.FileSets.ReplaceEmbeddedByPhysical<DemoDomainModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Hua.Abp.Demo.Domain"));
|
|
options.FileSets.ReplaceEmbeddedByPhysical<DemoApplicationContractsModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Hua.Abp.Demo.Application.Contracts"));
|
|
options.FileSets.ReplaceEmbeddedByPhysical<DemoApplicationModule>(Path.Combine(hostingEnvironment.ContentRootPath, $"..{Path.DirectorySeparatorChar}Hua.Abp.Demo.Application"));
|
|
});
|
|
}
|
|
}
|
|
|
|
private void ConfigureConventionalControllers()
|
|
{
|
|
Configure<AbpAspNetCoreMvcOptions>(options =>
|
|
{
|
|
options.ConventionalControllers.Create(typeof(DemoApplicationModule).Assembly);
|
|
});
|
|
}
|
|
|
|
private static void ConfigureSwagger(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
context.Services.AddAbpSwaggerGenWithOidc(
|
|
configuration["AuthServer:Authority"]!,
|
|
["Demo"],
|
|
[AbpSwaggerOidcFlows.AuthorizationCode],
|
|
null,
|
|
options =>
|
|
{
|
|
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Demo API", Version = "v1" });
|
|
options.DocInclusionPredicate((docName, description) => true);
|
|
options.CustomSchemaIds(type => type.FullName);
|
|
});
|
|
}
|
|
|
|
private void ConfigureCors(ServiceConfigurationContext context, IConfiguration configuration)
|
|
{
|
|
context.Services.AddCors(options =>
|
|
{
|
|
options.AddDefaultPolicy(builder =>
|
|
{
|
|
builder
|
|
.WithOrigins(
|
|
configuration["App:CorsOrigins"]?
|
|
.Split(",", StringSplitOptions.RemoveEmptyEntries)
|
|
.Select(o => o.Trim().RemovePostFix("/"))
|
|
.ToArray() ?? Array.Empty<string>()
|
|
)
|
|
.WithAbpExposedHeaders()
|
|
.SetIsOriginAllowedToAllowWildcardSubdomains()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod()
|
|
.AllowCredentials();
|
|
});
|
|
});
|
|
}
|
|
|
|
private void ConfigureHealthChecks(ServiceConfigurationContext context)
|
|
{
|
|
context.Services.AddDemoHealthChecks();
|
|
}
|
|
|
|
|
|
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
|
{
|
|
var app = context.GetApplicationBuilder();
|
|
var env = context.GetEnvironment();
|
|
|
|
app.UseForwardedHeaders();
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
|
|
app.UseAbpRequestLocalization();
|
|
|
|
if (!env.IsDevelopment())
|
|
{
|
|
app.UseErrorPage();
|
|
}
|
|
|
|
app.UseRouting();
|
|
app.MapAbpStaticAssets();
|
|
app.UseAbpStudioLink();
|
|
app.UseAbpSecurityHeaders();
|
|
app.UseCors();
|
|
app.UseAuthentication();
|
|
app.UseAbpOpenIddictValidation();
|
|
|
|
if (MultiTenancyConsts.IsEnabled)
|
|
{
|
|
app.UseMultiTenancy();
|
|
}
|
|
|
|
app.UseUnitOfWork();
|
|
app.UseDynamicClaims();
|
|
app.UseAuthorization();
|
|
|
|
app.UseSwagger();
|
|
app.UseAbpSwaggerUI(options =>
|
|
{
|
|
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Demo API");
|
|
|
|
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
|
|
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
|
|
});
|
|
app.UseAuditing();
|
|
app.UseAbpSerilogEnrichers();
|
|
app.UseConfiguredEndpoints();
|
|
}
|
|
}
|