UseIdentity now requires AddIdentity

This commit is contained in:
Hao Kung 2015-08-20 13:31:06 -07:00
parent cfb4a125bc
commit ab29d5f5e5
6 changed files with 56 additions and 11 deletions

View File

@ -4,6 +4,7 @@
using System;
using Microsoft.AspNet.Identity;
using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.Builder
{
@ -23,6 +24,13 @@ namespace Microsoft.AspNet.Builder
{
throw new ArgumentNullException(nameof(app));
}
var marker = app.ApplicationServices.GetService<IdentityMarkerService>();
if (marker == null)
{
throw new InvalidOperationException(Resources.MustCallAddIdentity);
}
app.UseCookieAuthentication(null, IdentityOptions.ExternalCookieAuthenticationScheme);
app.UseCookieAuthentication(null, IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme);
app.UseCookieAuthentication(null, IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme);

View File

@ -0,0 +1,10 @@
// 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.
namespace Microsoft.AspNet.Identity
{
/// <summary>
/// Used to verify AddIdentity was called on a ServiceCollection
/// </summary>
public class IdentityMarkerService { }
}

View File

@ -83,18 +83,19 @@ namespace Microsoft.Framework.DependencyInjection
services.AddAuthentication();
// Identity services
services.TryAdd(ServiceDescriptor.Scoped<IUserValidator<TUser>, UserValidator<TUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>());
services.TryAdd(ServiceDescriptor.Scoped<ILookupNormalizer, UpperInvariantLookupNormalizer>());
services.TryAdd(ServiceDescriptor.Scoped<IRoleValidator<TRole>, RoleValidator<TRole>>());
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.TryAdd(ServiceDescriptor.Scoped<IdentityErrorDescriber, IdentityErrorDescriber>());
services.TryAdd(ServiceDescriptor.Scoped<ISecurityStampValidator, SecurityStampValidator<TUser>>());
services.TryAdd(ServiceDescriptor.Scoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>());
services.TryAdd(ServiceDescriptor.Scoped<UserManager<TUser>, UserManager<TUser>>());
services.TryAdd(ServiceDescriptor.Scoped<SignInManager<TUser>, SignInManager<TUser>>());
services.TryAdd(ServiceDescriptor.Scoped<RoleManager<TRole>, RoleManager<TRole>>());
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)
{

View File

@ -218,6 +218,22 @@ namespace Microsoft.AspNet.Identity
return GetString("LoginAlreadyAssociated");
}
/// <summary>
/// AddIdentity must be called on the service collection.
/// </summary>
internal static string MustCallAddIdentity
{
get { return GetString("MustCallAddIdentity"); }
}
/// <summary>
/// AddIdentity must be called on the service collection.
/// </summary>
internal static string FormatMustCallAddIdentity()
{
return GetString("MustCallAddIdentity");
}
/// <summary>
/// No IUserTokenProvider named '{0}' is registered.
/// </summary>

View File

@ -169,6 +169,10 @@
<value>A user with this login already exists.</value>
<comment>Error when a login already linked</comment>
</data>
<data name="MustCallAddIdentity" xml:space="preserve">
<value>AddIdentity must be called on the service collection.</value>
<comment>Error when AddIdentity is not called</comment>
</data>
<data name="NoTokenProvider" xml:space="preserve">
<value>No IUserTokenProvider named '{0}' is registered.</value>
<comment>Error when there is no IUserTokenProvider</comment>

View File

@ -27,6 +27,12 @@ namespace Microsoft.AspNet.Identity.InMemory
{
const string TestPassword = "1qaz!QAZ";
[Fact]
public void UseIdentityThrowsWithoutAddIdentity()
{
Assert.Throws<InvalidOperationException>(() => TestServer.Create(app => app.UseIdentity()));
}
[Fact]
public async Task CanChangePasswordOptions()
{