Skip to content

Instantly share code, notes, and snippets.

@svenrog
Created January 31, 2022 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenrog/e02c09975bba545d3e8bd5a69aaa05a1 to your computer and use it in GitHub Desktop.
Save svenrog/e02c09975bba545d3e8bd5a69aaa05a1 to your computer and use it in GitHub Desktop.
Additional ApplicationBuilderExtensions for AspNetCore Identity in Optimizely 12
using Avensia.Storefront.Starter.Infrastructure.Security;
using EPiServer.Cms.UI.AspNetIdentity;
using EPiServer.Data.SchemaUpdates;
using EPiServer.Notification;
using EPiServer.Security;
using EPiServer.Shell.Security;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System;
// TODO: Use your own namespace
using YourProject.Infrastructure.Security;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ApplicationBuilderExtensions
{
public static IServiceCollection AddCmsAspNetIdentity<TUser>
(
this IServiceCollection services,
Action<ApplicationOptions> applicationOptions = null,
Action<IdentityOptions> configureIdentity = null,
Action<IdentityBuilder> configureIdentityBuilder = null,
Action<SqlServerDbContextOptionsBuilder> configureSqlBuilder = null
)
where TUser : IdentityUser, IUIUser, new()
{
if (applicationOptions != null)
{
services.Configure(applicationOptions);
}
services.AddDbContext<ApplicationDbContext<TUser>>((provider, options) =>
{
options.UseSqlServer(provider.GetService<ApplicationOptions>()!.ConnectionStringOptions.ConnectionString, configureSqlBuilder);
}, ServiceLifetime.Transient);
var identityBuilder = services.AddIdentity<TUser, IdentityRole>(options =>
{
options.Stores.MaxLengthForKeys = 128;
});
identityBuilder.AddEntityFrameworkStores<ApplicationDbContext<TUser>>()
.AddDefaultTokenProviders();
configureIdentityBuilder?.Invoke(identityBuilder);
services.AddServiceAccessor<ApplicationUserProvider<TUser>>();
services.AddServiceAccessor<ApplicationRoleProvider<TUser>>();
services.AddServiceAccessor<RoleManager<IdentityRole>>();
services.AddServiceAccessor<ApplicationUserManager<TUser>>();
services.AddServiceAccessor<ApplicationSignInManager<TUser>>();
services.AddTransient<SecurityEntityProvider, AspNetIdentitySecurityEntityProvider<TUser>>();
services.Forward<SecurityEntityProvider, IQueryableNotificationUsers>();
services.TryAddTransient<UIUserProvider, ApplicationUserProvider<TUser>>();
services.TryAddTransient<UIRoleProvider, ApplicationRoleProvider<TUser>>();
services.TryAddTransient<UIUserManager, ApplicationUIUserManager<TUser>>();
services.TryAddTransient<UISignInManager, ApplicationUISignInManager<TUser>>();
services.AddSingleton<ISchemaUpdater, AspNetIdentitySchemaUpdater<TUser>>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<ApplicationOptions>, ApplicationOptionsPostConfigurer>());
ConfigIdentityOptions(services, configureIdentity);
return services;
}
private static void ConfigIdentityOptions(IServiceCollection services, Action<IdentityOptions> configureIdentity)
{
if (configureIdentity != null)
{
services.Configure(configureIdentity);
return;
}
services.Configure(delegate (IdentityOptions options)
{
options.Password.RequiredLength = 6;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = true;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5.0);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
options.User.RequireUniqueEmail = true;
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment