// 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
{
///
/// Contains extension methods to for configuring identity services.
///
public static class IdentityServiceCollectionExtensions
{
///
/// Adds the default identity system configuration for the specified User and Role types.
///
/// The type representing a User in the system.
/// The type representing a Role in the system.
/// The services available in the application.
/// An for creating and configuring the identity system.
public static IdentityBuilder AddIdentity(
this IServiceCollection services)
where TUser : class
where TRole : class
{
return services.AddIdentity(setupAction: null);
}
///
/// Adds and configures the identity system for the specified User and Role types.
///
/// The type representing a User in the system.
/// The type representing a Role in the system.
/// The services available in the application.
/// An action to configure the .
/// An for creating and configuring the identity system.
public static IdentityBuilder AddIdentity(
this IServiceCollection services,
Action 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();
services.TryAddScoped, UserValidator>();
services.TryAddScoped, PasswordValidator>();
services.TryAddScoped, PasswordHasher>();
services.TryAddScoped();
services.TryAddScoped, RoleValidator>();
// No interface for the error describer so we can add errors without rev'ing the interface
services.TryAddScoped();
services.TryAddScoped>();
services.TryAddScoped, UserClaimsPrincipalFactory>();
services.TryAddScoped, UserManager>();
services.TryAddScoped, SignInManager>();
services.TryAddScoped, RoleManager>();
if (setupAction != null)
{
services.Configure(setupAction);
}
return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
}
}
}