mirror of
https://github.com/bitwarden/server.git
synced 2026-02-03 15:45:19 +08:00
50 lines
2.3 KiB
C#
50 lines
2.3 KiB
C#
|
|
using System;
|
|||
|
|
using Microsoft.AspNetCore.Http;
|
|||
|
|
using Microsoft.AspNetCore.Identity;
|
|||
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|||
|
|
|
|||
|
|
namespace Microsoft.Extensions.DependencyInjection
|
|||
|
|
{
|
|||
|
|
// 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);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static IdentityBuilder AddIdentityWithoutCookieAuth<TUser, TRole>(
|
|||
|
|
this IServiceCollection services,
|
|||
|
|
Action<IdentityOptions> setupAction)
|
|||
|
|
where TUser : class
|
|||
|
|
where TRole : class
|
|||
|
|
{
|
|||
|
|
// Hosting doesn't add IHttpContextAccessor by default
|
|||
|
|
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|||
|
|
// Identity services
|
|||
|
|
services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
|
|||
|
|
services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
|
|||
|
|
services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
|
|||
|
|
services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
|
|||
|
|
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<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
|
|||
|
|
services.TryAddScoped<UserManager<TUser>, AspNetUserManager<TUser>>();
|
|||
|
|
services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
|
|||
|
|
services.TryAddScoped<RoleManager<TRole>, AspNetRoleManager<TRole>>();
|
|||
|
|
|
|||
|
|
if(setupAction != null)
|
|||
|
|
{
|
|||
|
|
services.Configure(setupAction);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|