AuthServerHostModule.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using AuthServer.Host.EntityFrameworkCore;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Cors;
  4. using Microsoft.AspNetCore.DataProtection;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using StackExchange.Redis;
  7. using System;
  8. using System.Linq;
  9. using Volo.Abp;
  10. using Volo.Abp.Account;
  11. using Volo.Abp.Account.Web;
  12. using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic;
  13. using Volo.Abp.Auditing;
  14. using Volo.Abp.AuditLogging.EntityFrameworkCore;
  15. using Volo.Abp.Autofac;
  16. using Volo.Abp.Data;
  17. using Volo.Abp.EntityFrameworkCore;
  18. using Volo.Abp.EntityFrameworkCore.MySQL;
  19. using Volo.Abp.Identity;
  20. using Volo.Abp.Identity.EntityFrameworkCore;
  21. using Volo.Abp.IdentityServer.EntityFrameworkCore;
  22. using Volo.Abp.Localization;
  23. using Volo.Abp.Modularity;
  24. using Volo.Abp.MultiTenancy;
  25. using Volo.Abp.PermissionManagement.EntityFrameworkCore;
  26. using Volo.Abp.SettingManagement.EntityFrameworkCore;
  27. using Volo.Abp.TenantManagement;
  28. using Volo.Abp.TenantManagement.EntityFrameworkCore;
  29. using Volo.Abp.Threading;
  30. namespace AuthServer.Host
  31. {
  32. [DependsOn(
  33. typeof(AbpAutofacModule),
  34. typeof(AbpPermissionManagementEntityFrameworkCoreModule),
  35. typeof(AbpAuditLoggingEntityFrameworkCoreModule),
  36. typeof(AbpSettingManagementEntityFrameworkCoreModule),
  37. typeof(AbpIdentityEntityFrameworkCoreModule),
  38. typeof(AbpIdentityApplicationContractsModule),
  39. typeof(AbpAccountApplicationModule),
  40. typeof(AbpIdentityServerEntityFrameworkCoreModule),
  41. typeof(AbpEntityFrameworkCoreMySQLModule),
  42. typeof(AbpAccountWebIdentityServerModule),
  43. typeof(AbpAspNetCoreMvcUiBasicThemeModule),
  44. typeof(AbpTenantManagementEntityFrameworkCoreModule),
  45. typeof(AbpTenantManagementApplicationContractsModule)
  46. )]
  47. public class AuthServerHostModule : AbpModule
  48. {
  49. private const string DefaultCorsPolicyName = "Default";
  50. public override void ConfigureServices(ServiceConfigurationContext context)
  51. {
  52. var configuration = context.Services.GetConfiguration();
  53. context.Services.AddAbpDbContext<AuthServerDbContext>(options =>
  54. {
  55. options.AddDefaultRepositories();
  56. });
  57. Configure<AbpMultiTenancyOptions>(options =>
  58. {
  59. options.IsEnabled = true;
  60. });
  61. Configure<AbpDbContextOptions>(options =>
  62. {
  63. options.UseSqlServer();
  64. });
  65. Configure<AbpLocalizationOptions>(options =>
  66. {
  67. options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "¼òÌåÖÐÎÄ"));
  68. });
  69. context.Services.AddCors(options =>
  70. {
  71. options.AddPolicy(DefaultCorsPolicyName,
  72. builder =>
  73. {
  74. builder.WithOrigins(configuration["CorsOrigins"]
  75. .Split(",", StringSplitOptions.RemoveEmptyEntries)
  76. .Select(o => o.RemovePostFix("/"))
  77. .ToArray())
  78. .WithAbpExposedHeaders()
  79. .SetIsOriginAllowedToAllowWildcardSubdomains()
  80. .AllowAnyHeader()
  81. .AllowAnyMethod()
  82. .AllowCredentials();
  83. });
  84. });
  85. context.Services.AddSameSiteCookiePolicy();
  86. //context.Services.AddStackExchangeRedisCache(options =>
  87. //{
  88. // options.Configuration = configuration["Redis:Configuration"];
  89. //});
  90. Configure<AbpAuditingOptions>(options =>
  91. {
  92. options.IsEnabledForGetRequests = true;
  93. options.ApplicationName = "AuthServer";
  94. });
  95. //TODO: ConnectionMultiplexer.Connect call has problem since redis may not be ready when this service has started!
  96. //var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
  97. //context.Services.AddDataProtection()
  98. // .PersistKeysToStackExchangeRedis(redis, "MsDemo-DataProtection-Keys");
  99. }
  100. public override void OnApplicationInitialization(ApplicationInitializationContext context)
  101. {
  102. var app = context.GetApplicationBuilder();
  103. app.UseCookiePolicy();
  104. app.UseCorrelationId();
  105. app.UseStaticFiles();
  106. app.UseRouting();
  107. app.UseCors(DefaultCorsPolicyName);
  108. app.UseAbpRequestLocalization();
  109. app.UseAuthentication();
  110. app.UseMultiTenancy();
  111. app.UseIdentityServer();
  112. app.UseAuthorization();
  113. app.UseAuditing();
  114. app.UseConfiguredEndpoints();
  115. AsyncHelper.RunSync(async () =>
  116. {
  117. using (var scope = context.ServiceProvider.CreateScope())
  118. {
  119. await scope.ServiceProvider
  120. .GetRequiredService<IDataSeeder>()
  121. .SeedAsync();
  122. }
  123. });
  124. }
  125. }
  126. }