117 lines
3.9 KiB
C#
117 lines
3.9 KiB
C#
using System.Reflection;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using xCommons.Extensions;
|
|
using xCommons.Models;
|
|
using xIds.Constants;
|
|
|
|
namespace xIds.Extensions
|
|
{
|
|
public static class IIdentityServerBuilderExtensions
|
|
{
|
|
/// <summary>
|
|
/// Add Support for Configured XDbProvider to IdentityServer
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <returns></returns>
|
|
public static IIdentityServerBuilder AddXDbProvider(
|
|
this IIdentityServerBuilder source,
|
|
IConfiguration configuration
|
|
)
|
|
{
|
|
//
|
|
// Retrieve DB Info ...
|
|
var dbInfo = configuration.GetXDbInfo();
|
|
dbInfo.OptionsBuilder = (optionsBuilder) =>
|
|
{
|
|
optionsBuilder.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
|
|
optionsBuilder.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
|
|
};
|
|
|
|
//
|
|
// Retrieve Connection String Names ...
|
|
var identityDbConnectionString = configuration.GetConnectionString(ConnectionStringNames.IDENTITY_CONNECTION_NAME);
|
|
var configDbConnectionString = configuration.GetConnectionString(ConnectionStringNames.CONFIGURATION_CONNECTION_NAME);
|
|
var persistedGrantDbConnectionString = configuration.GetConnectionString(ConnectionStringNames.PRESISTED_GRANTS_CONNECTION_NAME);
|
|
|
|
//
|
|
// Add Operational Store ...
|
|
source.AddOperationalStore(opt =>
|
|
{
|
|
opt.ConfigureDbContext =
|
|
builder =>
|
|
{
|
|
//
|
|
builder.PrepareXDbContextOptionsBuilder(
|
|
dbInfo,
|
|
persistedGrantDbConnectionString
|
|
);
|
|
|
|
//
|
|
builder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
|
};
|
|
|
|
//
|
|
// this enables automatic token cleanup. this is optional.
|
|
opt.EnableTokenCleanup = true;
|
|
opt.TokenCleanupInterval = 3600;
|
|
});
|
|
|
|
//
|
|
// Add Configuration Store ...
|
|
source.AddConfigurationStore(opt =>
|
|
{
|
|
opt.ConfigureDbContext =
|
|
builder =>
|
|
{
|
|
//
|
|
builder.PrepareXDbContextOptionsBuilder(
|
|
dbInfo,
|
|
configDbConnectionString
|
|
);
|
|
};
|
|
});
|
|
|
|
//
|
|
return source;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set Configured Certificate File to IdentityServer
|
|
/// </summary>
|
|
/// <param name="builder"></param>
|
|
/// <param name="certificate"></param>
|
|
/// <returns></returns>
|
|
public static IIdentityServerBuilder LoadSigningCredentialFrom(
|
|
this IIdentityServerBuilder builder,
|
|
XCertificate certificate
|
|
)
|
|
{
|
|
//
|
|
if (!certificate.IsNull() &&
|
|
!certificate.Path.IsNullOrEmpty() &&
|
|
!certificate.Secret.IsNullOrEmpty()
|
|
)
|
|
{
|
|
try
|
|
{
|
|
builder.AddSigningCredential(new X509Certificate2(certificate.Path, certificate.Secret));
|
|
}
|
|
catch
|
|
{
|
|
builder.AddDeveloperSigningCredential();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
builder.AddDeveloperSigningCredential();
|
|
}
|
|
|
|
//
|
|
return builder;
|
|
}
|
|
}
|
|
} |