This commit is contained in:
Paul Schneider 2026-04-20 00:35:51 +01:00
commit 6cc0c519d2
15 changed files with 81 additions and 187 deletions

View file

@ -65,8 +65,9 @@ internal class Program
new() { ValidateAudience = false, RoleClaimType = Constants.RoleClaimType };
options.MapInboundClaims = true;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<ITrueEmailSender, MailSender>()

View file

@ -70,7 +70,7 @@ internal class Program
new() { ValidateAudience = false, RoleClaimType = Constants.RoleClaimType };
options.MapInboundClaims = true;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString(Constants.YavscConnectionStringName)));

View file

@ -85,18 +85,9 @@ public static class HostingExtensions
options.ResourcesPath = "Resources";
}).AddDataAnnotationsLocalization();
bool useTestEmailSender = builder.Configuration.GetValue<bool>("UseTestEmailSender", false);
if (useTestEmailSender)
{
services.AddTransient<ITrueEmailSender, TestMailSender>()
.AddTransient<Microsoft.AspNetCore.Identity.UI.Services.IEmailSender, TestMailSender>();
}
else
{
services.AddTransient<ITrueEmailSender, MailSender>()
.AddTransient<Microsoft.AspNetCore.Identity.UI.Services.IEmailSender, MailSender>();
}
services.AddTransient<IYavscMessageSender, YavscMessageSender>()
.AddTransient<IBillingService, BillingService>()
@ -147,21 +138,12 @@ public static class HostingExtensions
public static IdentityBuilder AddIdentityDBAndStores(this WebApplicationBuilder builder)
{
IServiceCollection services = builder.Services;
bool useInMemory = builder.Configuration.GetValue<bool>("UseInMemoryDatabase", false);
if (useInMemory)
services.AddDbContext<ApplicationDbContext>(options =>
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite("Data Source=file::memory:?cache=shared"));
}
else
{
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString(Constants.YavscConnectionStringName),
options => options.MigrationsAssembly(typeof(Program).Assembly));
});
}
options.UseNpgsql(builder.Configuration.GetConnectionString(Constants.YavscConnectionStringName),
options => options.MigrationsAssembly(typeof(Program).Assembly));
});
var identityBuilder = services.AddIdentity<ApplicationUser, IdentityRole>(
options =>
@ -280,8 +262,8 @@ public static class HostingExtensions
});
var migrationsAssembly = typeof(Program).GetTypeInfo().Assembly.GetName().Name;
var connectionString = builder.Configuration.GetConnectionString(Constants.YavscConnectionStringName);
bool useInMemory = builder.Configuration.GetValue<bool>("UseInMemoryDatabase", false);
string sqliteInMemoryConnectionString = "Data Source=file::memory:?cache=shared";
string sqliteConnectionString = $"Data Source={Path.Combine(Path.GetTempPath(), "yavsc_test.db")}";
var identityServerBuilder = builder.Services.AddIdentityServer(options =>
{
@ -301,40 +283,15 @@ public static class HostingExtensions
.AddResourceStore<ResourceStore>()
.AddConfigurationStore(options =>
{
if (useInMemory)
{
options.ConfigureDbContext = b => b.UseSqlite(sqliteInMemoryConnectionString);
}
else
{
options.ConfigureDbContext = b => b.UseNpgsql(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly))
.UseSeeding((context, _) =>
{
foreach (String scope in new string[] { "blog", "admin", "contract", "com"})
{
var testBlog = context.Set<ApiScope>().FirstOrDefault(b => b.Name == scope);
if (testBlog == null)
{
context.Set<ApiScope>().Add(new ApiScope { Name = scope });
context.SaveChanges();
}
}
});
}
options.ConfigureDbContext = b => b.UseNpgsql(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly))
.UseSeeding(EnsureDefaultApplicationScopes());
})
.AddOperationalStore(options =>
{
if (useInMemory)
{
options.ConfigureDbContext = b => b.UseSqlite(sqliteInMemoryConnectionString);
}
else
{
options.ConfigureDbContext = b => b.UseNpgsql(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
}
options.ConfigureDbContext = b => b.UseNpgsql(connectionString,
sql => sql.MigrationsAssembly(migrationsAssembly));
});
if (builder.Environment.IsDevelopment())
@ -344,6 +301,23 @@ public static class HostingExtensions
return identityServerBuilder;
}
private static Action<DbContext, bool> EnsureDefaultApplicationScopes()
{
return (context, _) =>
{
foreach (String scope in new string[] { "blog", "admin", "contract", "com" })
{
var existentScope = context.Set<ApiScope>().FirstOrDefault(b => b.Name == scope);
if (existentScope == null)
{
context.Set<ApiScope>().Add(new ApiScope { Name = scope });
context.SaveChanges();
}
}
};
}
private static void ConfigureRequestLocalization(IServiceCollection services)
{
services.Configure<RequestLocalizationOptions>(options =>

View file

@ -31,8 +31,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" />
<PackageReference Include="Google.Apis.Compute.v1" />

View file

@ -47,9 +47,9 @@ namespace Yavsc.Services
/// <returns>a MessageWithPayloadResponse,
/// <c>bool somethingsent = (response.failure == 0 &amp;&amp; response.success > 0)</c>
/// </returns>
public async Task SendEmailAsync(string email, string subject, string htmlMessage)
public Task SendEmailAsync(string email, string subject, string htmlMessage)
{
await SendEmailAsync("", email, subject, htmlMessage);
return SendEmailAsync("", email, subject, htmlMessage);
}
public async Task<string> SendEmailAsync(string name, string email, string subject, string htmlMessage)
@ -71,8 +71,9 @@ namespace Yavsc.Services
);
using (SmtpClient sc = new())
{
sc.Timeout = 30000;
sc.Connect(
smtpSettings.Server,
smtpSettings.Host,
smtpSettings.Port,
SecureSocketOptions.Auto
);

View file

@ -1,30 +0,0 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.Extensions.Logging;
using Yavsc.Interface;
namespace Yavsc.Services
{
public class TestMailSender : ITrueEmailSender, IEmailSender
{
private readonly ILogger<TestMailSender> logger;
public TestMailSender(ILoggerFactory loggerFactory)
{
logger = loggerFactory.CreateLogger<TestMailSender>();
}
public Task SendEmailAsync(string email, string subject, string htmlMessage)
{
logger.LogInformation("[TestMailSender] SendEmailAsync to {Email} subject={Subject}", email, subject);
return Task.CompletedTask;
}
public Task<string> SendEmailAsync(string name, string email, string subject, string htmlMessage)
{
logger.LogInformation("[TestMailSender] SendEmailAsync to {Email} subject={Subject} name={Name}", email, subject, name);
return Task.FromResult($"test-message-{Guid.NewGuid()}");
}
}
}

View file

@ -2,17 +2,13 @@ namespace Yavsc.Settings
{
public class SmtpSettings
{
public string Server { get; set; }
public string Host
{
get => Server;
set => Server = value;
get ;
set ;
}
public int Port { get; set; }
public string SenderName { get; set; }
public string SenderEmail { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}