Remove ConfigureDefaultOptions
This commit is contained in:
parent
66d69f7ae5
commit
55e85e09ce
|
|
@ -8,7 +8,6 @@ using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
using Microsoft.Extensions.Options.Infrastructure;
|
|
||||||
|
|
||||||
namespace Microsoft.Extensions.DependencyInjection
|
namespace Microsoft.Extensions.DependencyInjection
|
||||||
{
|
{
|
||||||
|
|
@ -17,13 +16,6 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class IdentityServiceCollectionExtensions
|
public static class IdentityServiceCollectionExtensions
|
||||||
{
|
{
|
||||||
internal class IdentityConfigureOptions : ConfigureDefaultOptions<IdentityOptions>
|
|
||||||
{
|
|
||||||
public IdentityConfigureOptions(IConfiguration config) :
|
|
||||||
base(options => config.GetSection("Microsoft:AspNetCore:Identity").Bind(options))
|
|
||||||
{ }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds the default identity system configuration for the specified User and Role types.
|
/// Adds the default identity system configuration for the specified User and Role types.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -119,7 +111,6 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
|
services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
|
||||||
services.TryAddScoped<RoleManager<TRole>, AspNetRoleManager<TRole>>();
|
services.TryAddScoped<RoleManager<TRole>, AspNetRoleManager<TRole>>();
|
||||||
|
|
||||||
services.AddSingleton<ConfigureDefaultOptions<IdentityOptions>, IdentityConfigureOptions>();
|
|
||||||
if (setupAction != null)
|
if (setupAction != null)
|
||||||
{
|
{
|
||||||
services.Configure(setupAction);
|
services.Configure(setupAction);
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="$(AspNetCoreVersion)" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="$(AspNetCoreVersion)" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="$(AspNetCoreVersion)" />
|
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="$(AspNetCoreVersion)" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="$(AspNetCoreVersion)" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.TaskCache.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
|
<PackageReference Include="Microsoft.Extensions.TaskCache.Sources" Version="$(AspNetCoreVersion)" PrivateAssets="All" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.Extensions.Options.Infrastructure;
|
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Identity.Test
|
namespace Microsoft.AspNetCore.Identity.Test
|
||||||
|
|
@ -39,79 +38,6 @@ namespace Microsoft.AspNetCore.Identity.Test
|
||||||
Assert.Equal("AspNet.Identity.SecurityStamp", options.ClaimsIdentity.SecurityStampClaimType);
|
Assert.Equal("AspNet.Identity.SecurityStamp", options.ClaimsIdentity.SecurityStampClaimType);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void IdentityOptionsFromConfig()
|
|
||||||
{
|
|
||||||
const string roleClaimType = "rolez";
|
|
||||||
const string usernameClaimType = "namez";
|
|
||||||
const string useridClaimType = "idz";
|
|
||||||
const string securityStampClaimType = "stampz";
|
|
||||||
|
|
||||||
var dic = new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"microsoft:aspnetcore:identity:claimsidentity:roleclaimtype", roleClaimType},
|
|
||||||
{"microsoft:aspnetcore:identity:claimsidentity:usernameclaimtype", usernameClaimType},
|
|
||||||
{"microsoft:aspnetcore:identity:claimsidentity:useridclaimtype", useridClaimType},
|
|
||||||
{"microsoft:aspnetcore:identity:claimsidentity:securitystampclaimtype", securityStampClaimType},
|
|
||||||
{"microsoft:aspnetcore:identity:user:requireUniqueEmail", "true"},
|
|
||||||
{"microsoft:aspnetcore:identity:password:RequiredLength", "10"},
|
|
||||||
{"microsoft:aspnetcore:identity:password:RequiredUniqueChars", "5"},
|
|
||||||
{"microsoft:aspnetcore:identity:password:RequireNonAlphanumeric", "false"},
|
|
||||||
{"microsoft:aspnetcore:identity:password:RequireUpperCase", "false"},
|
|
||||||
{"microsoft:aspnetcore:identity:password:RequireDigit", "false"},
|
|
||||||
{"microsoft:aspnetcore:identity:password:RequireLowerCase", "false"},
|
|
||||||
{"microsoft:aspnetcore:identity:lockout:AllowedForNewUsers", "FALSe"},
|
|
||||||
{"microsoft:aspnetcore:identity:lockout:MaxFailedAccessAttempts", "1000"}
|
|
||||||
};
|
|
||||||
var builder = new ConfigurationBuilder();
|
|
||||||
builder.AddInMemoryCollection(dic);
|
|
||||||
var config = builder.Build();
|
|
||||||
Assert.Equal(roleClaimType, config["microsoft:aspnetcore:identity:claimsidentity:roleclaimtype"]);
|
|
||||||
|
|
||||||
var services = new ServiceCollection()
|
|
||||||
.AddSingleton<IConfiguration>(config)
|
|
||||||
.AddSingleton<IConfigureOptions<IdentityOptions>, ConfigureDefaults<IdentityOptions>>();
|
|
||||||
services.AddIdentity<TestUser,TestRole>();
|
|
||||||
var accessor = services.BuildServiceProvider().GetRequiredService<IOptions<IdentityOptions>>();
|
|
||||||
Assert.NotNull(accessor);
|
|
||||||
var options = accessor.Value;
|
|
||||||
Assert.Equal(roleClaimType, options.ClaimsIdentity.RoleClaimType);
|
|
||||||
Assert.Equal(useridClaimType, options.ClaimsIdentity.UserIdClaimType);
|
|
||||||
Assert.Equal(usernameClaimType, options.ClaimsIdentity.UserNameClaimType);
|
|
||||||
Assert.Equal(securityStampClaimType, options.ClaimsIdentity.SecurityStampClaimType);
|
|
||||||
Assert.True(options.User.RequireUniqueEmail);
|
|
||||||
Assert.Equal("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+", options.User.AllowedUserNameCharacters);
|
|
||||||
Assert.False(options.Password.RequireDigit);
|
|
||||||
Assert.False(options.Password.RequireLowercase);
|
|
||||||
Assert.False(options.Password.RequireNonAlphanumeric);
|
|
||||||
Assert.False(options.Password.RequireUppercase);
|
|
||||||
Assert.Equal(10, options.Password.RequiredLength);
|
|
||||||
Assert.Equal(5, options.Password.RequiredUniqueChars);
|
|
||||||
Assert.False(options.Lockout.AllowedForNewUsers);
|
|
||||||
Assert.Equal(1000, options.Lockout.MaxFailedAccessAttempts);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void IdentityOptionsActionOverridesConfig()
|
|
||||||
{
|
|
||||||
var dic = new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"microsoft:aspnetcore:identity:user:requireUniqueEmail", "true"},
|
|
||||||
{"microsoft:aspnetcore:identity:lockout:MaxFailedAccessAttempts", "1000"}
|
|
||||||
};
|
|
||||||
var builder = new ConfigurationBuilder();
|
|
||||||
builder.AddInMemoryCollection(dic);
|
|
||||||
var services = new ServiceCollection()
|
|
||||||
.AddSingleton<IConfiguration>(builder.Build())
|
|
||||||
.AddSingleton<IConfigureOptions<IdentityOptions>, ConfigureDefaults<IdentityOptions>>();
|
|
||||||
services.AddIdentity<TestUser, TestRole>(o => { o.User.RequireUniqueEmail = false; o.Lockout.MaxFailedAccessAttempts++; });
|
|
||||||
var accessor = services.BuildServiceProvider().GetRequiredService<IOptions<IdentityOptions>>();
|
|
||||||
Assert.NotNull(accessor);
|
|
||||||
var options = accessor.Value;
|
|
||||||
Assert.False(options.User.RequireUniqueEmail);
|
|
||||||
Assert.Equal(1001, options.Lockout.MaxFailedAccessAttempts);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CanCustomizeIdentityOptions()
|
public void CanCustomizeIdentityOptions()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue