75 lines
3.8 KiB
C#
75 lines
3.8 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using Microsoft.AspNet.Identity;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
|
|
namespace Microsoft.Extensions.DependencyInjection
|
|
{
|
|
/// <summary>
|
|
/// Contains extension methods to <see cref="IServiceCollection"/> for configuring identity services.
|
|
/// </summary>
|
|
public static class IdentityServiceCollectionExtensions
|
|
{
|
|
/// <summary>
|
|
/// Adds the default identity system configuration for the specified User and Role types.
|
|
/// </summary>
|
|
/// <typeparam name="TUser">The type representing a User in the system.</typeparam>
|
|
/// <typeparam name="TRole">The type representing a Role in the system.</typeparam>
|
|
/// <param name="services">The services available in the application.</param>
|
|
/// <returns>An <see cref="IdentityBuilder"/> for creating and configuring the identity system.</returns>
|
|
public static IdentityBuilder AddIdentity<TUser, TRole>(
|
|
this IServiceCollection services)
|
|
where TUser : class
|
|
where TRole : class
|
|
{
|
|
return services.AddIdentity<TUser, TRole>(setupAction: null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds and configures the identity system for the specified User and Role types.
|
|
/// </summary>
|
|
/// <typeparam name="TUser">The type representing a User in the system.</typeparam>
|
|
/// <typeparam name="TRole">The type representing a Role in the system.</typeparam>
|
|
/// <param name="services">The services available in the application.</param>
|
|
/// <param name="setupAction">An action to configure the <see cref="IdentityOptions"/>.</param>
|
|
/// <returns>An <see cref="IdentityBuilder"/> for creating and configuring the identity system.</returns>
|
|
public static IdentityBuilder AddIdentity<TUser, TRole>(
|
|
this IServiceCollection services,
|
|
Action<IdentityOptions> setupAction)
|
|
where TUser : class
|
|
where TRole : class
|
|
{
|
|
// Services used by identity
|
|
services.AddOptions();
|
|
services.AddAuthentication(options =>
|
|
{
|
|
// This is the Default value for ExternalCookieAuthenticationScheme
|
|
options.SignInScheme = new IdentityCookieOptions().ExternalCookieAuthenticationScheme;
|
|
});
|
|
|
|
// Identity services
|
|
services.TryAddSingleton<IdentityMarkerService>();
|
|
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>, UserManager<TUser>>();
|
|
services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
|
|
services.TryAddScoped<RoleManager<TRole>, RoleManager<TRole>>();
|
|
|
|
if (setupAction != null)
|
|
{
|
|
services.Configure(setupAction);
|
|
}
|
|
|
|
return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
|
|
}
|
|
}
|
|
} |