// 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.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.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
=> 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.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddCookie(IdentityConstants.ApplicationScheme, o =>
{
o.LoginPath = new PathString("/Account/Login");
o.Events = new CookieAuthenticationEvents
{
OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
};
})
.AddCookie(IdentityConstants.ExternalScheme, o =>
{
o.Cookie.Name = IdentityConstants.ExternalScheme;
o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
})
.AddCookie(IdentityConstants.TwoFactorRememberMeScheme, o =>
{
o.Cookie.Name = IdentityConstants.TwoFactorRememberMeScheme;
o.Events = new CookieAuthenticationEvents
{
OnValidatePrincipal = SecurityStampValidator.ValidateAsync
};
})
.AddCookie(IdentityConstants.TwoFactorUserIdScheme, o =>
{
o.Cookie.Name = IdentityConstants.TwoFactorUserIdScheme;
o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
});
// Hosting doesn't add IHttpContextAccessor by default
services.AddHttpContextAccessor();
// Identity services
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>();
services.TryAddScoped, UserClaimsPrincipalFactory>();
services.TryAddScoped, AspNetUserManager>();
services.TryAddScoped, SignInManager>();
services.TryAddScoped, AspNetRoleManager>();
if (setupAction != null)
{
services.Configure(setupAction);
}
return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
}
///
/// Configures the application cookie.
///
/// The services available in the application.
/// An action to configure the .
/// The services.
public static IServiceCollection ConfigureApplicationCookie(this IServiceCollection services, Action configure)
=> services.Configure(IdentityConstants.ApplicationScheme, configure);
///
/// Configure the external cookie.
///
/// The services available in the application.
/// An action to configure the .
/// The services.
public static IServiceCollection ConfigureExternalCookie(this IServiceCollection services, Action configure)
=> services.Configure(IdentityConstants.ExternalScheme, configure);
}
}