diff --git a/src/Microsoft.AspNet.Identity/BuilderExtensions.cs b/src/Microsoft.AspNet.Identity/BuilderExtensions.cs index f7931f356a..2f5159da32 100644 --- a/src/Microsoft.AspNet.Identity/BuilderExtensions.cs +++ b/src/Microsoft.AspNet.Identity/BuilderExtensions.cs @@ -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(); + if (marker == null) + { + throw new InvalidOperationException(Resources.MustCallAddIdentity); + } + app.UseCookieAuthentication(null, IdentityOptions.ExternalCookieAuthenticationScheme); app.UseCookieAuthentication(null, IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme); app.UseCookieAuthentication(null, IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme); diff --git a/src/Microsoft.AspNet.Identity/IdentityMarkerService.cs b/src/Microsoft.AspNet.Identity/IdentityMarkerService.cs new file mode 100644 index 0000000000..27bc77fc93 --- /dev/null +++ b/src/Microsoft.AspNet.Identity/IdentityMarkerService.cs @@ -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 +{ + /// + /// Used to verify AddIdentity was called on a ServiceCollection + /// + public class IdentityMarkerService { } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Identity/IdentityServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Identity/IdentityServiceCollectionExtensions.cs index c3f60840e2..303144b717 100644 --- a/src/Microsoft.AspNet.Identity/IdentityServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Identity/IdentityServiceCollectionExtensions.cs @@ -83,18 +83,19 @@ namespace Microsoft.Framework.DependencyInjection services.AddAuthentication(); // Identity services - services.TryAdd(ServiceDescriptor.Scoped, UserValidator>()); - services.TryAdd(ServiceDescriptor.Scoped, PasswordValidator>()); - services.TryAdd(ServiceDescriptor.Scoped, PasswordHasher>()); - services.TryAdd(ServiceDescriptor.Scoped()); - services.TryAdd(ServiceDescriptor.Scoped, RoleValidator>()); + 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.TryAdd(ServiceDescriptor.Scoped()); - services.TryAdd(ServiceDescriptor.Scoped>()); - services.TryAdd(ServiceDescriptor.Scoped, UserClaimsPrincipalFactory>()); - services.TryAdd(ServiceDescriptor.Scoped, UserManager>()); - services.TryAdd(ServiceDescriptor.Scoped, SignInManager>()); - services.TryAdd(ServiceDescriptor.Scoped, RoleManager>()); + services.TryAddScoped(); + services.TryAddScoped>(); + services.TryAddScoped, UserClaimsPrincipalFactory>(); + services.TryAddScoped, UserManager>(); + services.TryAddScoped, SignInManager>(); + services.TryAddScoped, RoleManager>(); if (setupAction != null) { diff --git a/src/Microsoft.AspNet.Identity/Properties/Resources.Designer.cs b/src/Microsoft.AspNet.Identity/Properties/Resources.Designer.cs index 85ec23501c..5319301867 100644 --- a/src/Microsoft.AspNet.Identity/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNet.Identity/Properties/Resources.Designer.cs @@ -218,6 +218,22 @@ namespace Microsoft.AspNet.Identity return GetString("LoginAlreadyAssociated"); } + /// + /// AddIdentity must be called on the service collection. + /// + internal static string MustCallAddIdentity + { + get { return GetString("MustCallAddIdentity"); } + } + + /// + /// AddIdentity must be called on the service collection. + /// + internal static string FormatMustCallAddIdentity() + { + return GetString("MustCallAddIdentity"); + } + /// /// No IUserTokenProvider named '{0}' is registered. /// diff --git a/src/Microsoft.AspNet.Identity/Resources.resx b/src/Microsoft.AspNet.Identity/Resources.resx index 558724645a..a630c32faa 100644 --- a/src/Microsoft.AspNet.Identity/Resources.resx +++ b/src/Microsoft.AspNet.Identity/Resources.resx @@ -169,6 +169,10 @@ A user with this login already exists. Error when a login already linked + + AddIdentity must be called on the service collection. + Error when AddIdentity is not called + No IUserTokenProvider named '{0}' is registered. Error when there is no IUserTokenProvider diff --git a/test/Microsoft.AspNet.Identity.InMemory.Test/FunctionalTest.cs b/test/Microsoft.AspNet.Identity.InMemory.Test/FunctionalTest.cs index fd53910d12..58ab902a35 100644 --- a/test/Microsoft.AspNet.Identity.InMemory.Test/FunctionalTest.cs +++ b/test/Microsoft.AspNet.Identity.InMemory.Test/FunctionalTest.cs @@ -27,6 +27,12 @@ namespace Microsoft.AspNet.Identity.InMemory { const string TestPassword = "1qaz!QAZ"; + [Fact] + public void UseIdentityThrowsWithoutAddIdentity() + { + Assert.Throws(() => TestServer.Create(app => app.UseIdentity())); + } + [Fact] public async Task CanChangePasswordOptions() {