Files
Umbraco-CMS/src/Umbraco.Web.Common/Extensions/UmbracoApplicationBuilder.Identity.cs
Callum Whyte b850cd3ee0 Add SetMemberSignInManager builder extension (#14761)
* Add SetMemberSignInManager builder extension

* Added required using statement

---------

Co-authored-by: Emma Garland <emma.garland@rocksolidknowledge.com>
2023-10-13 12:30:13 +01:00

63 lines
3.0 KiB
C#

using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Web.Common.Security;
namespace Umbraco.Extensions;
public static partial class UmbracoApplicationBuilderExtensions
{
public static IUmbracoBuilder SetBackOfficeUserManager<TUserManager>(this IUmbracoBuilder builder)
where TUserManager : UserManager<BackOfficeIdentityUser>, IBackOfficeUserManager
{
Type customType = typeof(TUserManager);
Type userManagerType = typeof(UserManager<BackOfficeIdentityUser>);
builder.Services.Replace(ServiceDescriptor.Scoped(typeof(IBackOfficeUserManager), customType));
builder.Services.AddScoped(customType, services => services.GetRequiredService(userManagerType));
builder.Services.Replace(ServiceDescriptor.Scoped(userManagerType, customType));
return builder;
}
public static IUmbracoBuilder SetBackOfficeUserStore<TUserStore>(this IUmbracoBuilder builder)
where TUserStore : BackOfficeUserStore
{
Type customType = typeof(TUserStore);
builder.Services.Replace(
ServiceDescriptor.Scoped(typeof(IUserStore<>).MakeGenericType(typeof(BackOfficeIdentityUser)), customType));
return builder;
}
public static IUmbracoBuilder SetMemberManager<TUserManager>(this IUmbracoBuilder builder)
where TUserManager : UserManager<MemberIdentityUser>, IMemberManager
{
Type customType = typeof(TUserManager);
Type userManagerType = typeof(UserManager<MemberIdentityUser>);
builder.Services.Replace(ServiceDescriptor.Scoped(typeof(IMemberManager), customType));
builder.Services.AddScoped(customType, services => services.GetRequiredService(userManagerType));
builder.Services.Replace(ServiceDescriptor.Scoped(userManagerType, customType));
return builder;
}
public static IUmbracoBuilder SetMemberSignInManager<TSignInManager>(this IUmbracoBuilder builder)
where TSignInManager : SignInManager<MemberIdentityUser>, IMemberSignInManager
{
Type customType = typeof(TSignInManager);
Type signInManagerType = typeof(SignInManager<MemberIdentityUser>);
builder.Services.Replace(ServiceDescriptor.Scoped(typeof(IMemberSignInManager), customType));
builder.Services.AddScoped(customType, services => services.GetRequiredService(signInManagerType));
builder.Services.Replace(ServiceDescriptor.Scoped(signInManagerType, customType));
return builder;
}
public static IUmbracoBuilder SetMemberUserStore<TUserStore>(this IUmbracoBuilder builder)
where TUserStore : MemberUserStore
{
Type customType = typeof(TUserStore);
builder.Services.Replace(
ServiceDescriptor.Scoped(typeof(IUserStore<>).MakeGenericType(typeof(MemberIdentityUser)), customType));
return builder;
}
}