Files
server/src/Core/Auth/Identity/CustomIdentityServiceCollectionExtensions.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

53 lines
2.4 KiB
C#
Raw Normal View History

// FIXME: Update this file to be null safe and then delete the line below
#nullable disable
using Bit.Core.Auth.Identity;
2017-10-06 14:02:28 -04:00
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection;
2022-08-29 16:06:55 -04:00
2017-10-06 14:02:28 -04:00
// ref: https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity/IdentityServiceCollectionExtensions.cs
public static class CustomIdentityServiceCollectionExtensions
{
public static IdentityBuilder AddIdentityWithoutCookieAuth<TUser, TRole>(
this IServiceCollection services)
where TUser : class
where TRole : class
{
return services.AddIdentityWithoutCookieAuth<TUser, TRole>(setupAction: null);
2022-08-29 16:06:55 -04:00
}
2017-10-06 14:02:28 -04:00
public static IdentityBuilder AddIdentityWithoutCookieAuth<TUser, TRole>(
this IServiceCollection services,
Action<IdentityOptions> setupAction)
where TUser : class
where TRole : class
{
2017-10-06 14:02:28 -04:00
// Hosting doesn't add IHttpContextAccessor by default
2020-01-10 08:33:13 -05:00
services.AddHttpContextAccessor();
2017-10-06 14:02:28 -04:00
// Identity services
services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
services.TryAddScoped<ILookupNormalizer, LowerInvariantLookupNormalizer>();
services.TryAddScoped<IRoleValidator<TRole>, RoleValidator<TRole>>();
// No interface for the error describer so we can add errors without rev'ing the interface
services.TryAddScoped<IdentityErrorDescriber>();
services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
services.TryAddScoped<ITwoFactorSecurityStampValidator, TwoFactorSecurityStampValidator<TUser>>();
services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
services.TryAddScoped<IUserConfirmation<TUser>, DefaultUserConfirmation<TUser>>();
2020-01-10 08:33:13 -05:00
services.TryAddScoped<UserManager<TUser>>();
2017-10-06 14:02:28 -04:00
services.TryAddScoped<SignInManager<TUser>>();
2020-01-10 08:33:13 -05:00
services.TryAddScoped<RoleManager<TRole>>();
2017-10-06 14:02:28 -04:00
if (setupAction != null)
2022-08-29 16:06:55 -04:00
{
2017-10-06 14:02:28 -04:00
services.Configure(setupAction);
}
2022-08-29 16:06:55 -04:00
2017-10-06 14:02:28 -04:00
return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
}
}