[2.0.1] Allow use of base identity pocos (#1385)

This commit is contained in:
Hao Kung 2017-08-24 14:21:09 -07:00 committed by GitHub
parent e36e681d54
commit b865d58786
8 changed files with 38 additions and 14 deletions

View File

@ -96,15 +96,16 @@ namespace Microsoft.Extensions.DependencyInjection
private static TypeInfo FindGenericBaseType(Type currentType, Type genericBaseType) private static TypeInfo FindGenericBaseType(Type currentType, Type genericBaseType)
{ {
var type = currentType.GetTypeInfo(); var type = currentType;
while (type.BaseType != null) while (type != null)
{ {
type = type.BaseType.GetTypeInfo(); var typeInfo = type.GetTypeInfo();
var genericType = type.IsGenericType ? type.GetGenericTypeDefinition() : null; var genericType = type.IsGenericType ? type.GetGenericTypeDefinition() : null;
if (genericType != null && genericType == genericBaseType) if (genericType != null && genericType == genericBaseType)
{ {
return type; return typeInfo;
} }
type = type.BaseType;
} }
return null; return null;
} }

View File

@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
= new ResourceManager("Microsoft.AspNetCore.Identity.EntityFrameworkCore.Resources", typeof(Resources).GetTypeInfo().Assembly); = new ResourceManager("Microsoft.AspNetCore.Identity.EntityFrameworkCore.Resources", typeof(Resources).GetTypeInfo().Assembly);
/// <summary> /// <summary>
/// AddEntityFrameworkStores can only be called with a role that derives from IdentityRole&lt;TKey, TUserRole, TRoleClaim&gt;. /// AddEntityFrameworkStores can only be called with a role that derives from IdentityRole&lt;TKey&gt;.
/// </summary> /// </summary>
internal static string NotIdentityRole internal static string NotIdentityRole
{ {
@ -19,13 +19,13 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
} }
/// <summary> /// <summary>
/// AddEntityFrameworkStores can only be called with a role that derives from IdentityRole&lt;TKey, TUserRole, TRoleClaim&gt;. /// AddEntityFrameworkStores can only be called with a role that derives from IdentityRole&lt;TKey&gt;.
/// </summary> /// </summary>
internal static string FormatNotIdentityRole() internal static string FormatNotIdentityRole()
=> GetString("NotIdentityRole"); => GetString("NotIdentityRole");
/// <summary> /// <summary>
/// AddEntityFrameworkStores can only be called with a user that derives from IdentityUser&lt;TKey, TUserClaim, TUserRole, TUserLogin, TUserToken&gt;. /// AddEntityFrameworkStores can only be called with a user that derives from IdentityUser&lt;TKey&gt;.
/// </summary> /// </summary>
internal static string NotIdentityUser internal static string NotIdentityUser
{ {
@ -33,7 +33,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
} }
/// <summary> /// <summary>
/// AddEntityFrameworkStores can only be called with a user that derives from IdentityUser&lt;TKey, TUserClaim, TUserRole, TUserLogin, TUserToken&gt;. /// AddEntityFrameworkStores can only be called with a user that derives from IdentityUser&lt;TKey&gt;.
/// </summary> /// </summary>
internal static string FormatNotIdentityUser() internal static string FormatNotIdentityUser()
=> GetString("NotIdentityUser"); => GetString("NotIdentityUser");

View File

@ -118,11 +118,11 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<data name="NotIdentityRole" xml:space="preserve"> <data name="NotIdentityRole" xml:space="preserve">
<value>AddEntityFrameworkStores can only be called with a role that derives from IdentityRole&lt;TKey, TUserRole, TRoleClaim&gt;.</value> <value>AddEntityFrameworkStores can only be called with a role that derives from IdentityRole&lt;TKey&gt;.</value>
<comment>error when the role does not derive from IdentityRole</comment> <comment>error when the role does not derive from IdentityRole</comment>
</data> </data>
<data name="NotIdentityUser" xml:space="preserve"> <data name="NotIdentityUser" xml:space="preserve">
<value>AddEntityFrameworkStores can only be called with a user that derives from IdentityUser&lt;TKey, TUserClaim, TUserRole, TUserLogin, TUserToken&gt;.</value> <value>AddEntityFrameworkStores can only be called with a user that derives from IdentityUser&lt;TKey&gt;.</value>
<comment>error when the user does not derive from IdentityUser</comment> <comment>error when the user does not derive from IdentityUser</comment>
</data> </data>
<data name="RoleNotFound" xml:space="preserve"> <data name="RoleNotFound" xml:space="preserve">

View File

@ -1,9 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder.Internal; using Microsoft.AspNetCore.Builder.Internal;
using Microsoft.AspNetCore.Identity.Test; using Microsoft.AspNetCore.Identity.Test;

View File

@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test
protected override void SetupIdentityServices(IServiceCollection services, object context) protected override void SetupIdentityServices(IServiceCollection services, object context)
{ {
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<TestDbContext>((TestDbContext)context); services.AddSingleton((TestDbContext)context);
services.AddLogging(); services.AddLogging();
services.AddSingleton<ILogger<UserManager<TUser>>>(new TestLogger<UserManager<TUser>>()); services.AddSingleton<ILogger<UserManager<TUser>>>(new TestLogger<UserManager<TUser>>());
services.AddSingleton<ILogger<RoleManager<TRole>>>(new TestLogger<RoleManager<TRole>>()); services.AddSingleton<ILogger<RoleManager<TRole>>>(new TestLogger<RoleManager<TRole>>());

View File

@ -59,5 +59,14 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test
// This used to throw // This used to throw
var builder = services.AddIdentity<GuidUser, GuidRole>().AddEntityFrameworkStores<TestDbContext>(); var builder = services.AddIdentity<GuidUser, GuidRole>().AddEntityFrameworkStores<TestDbContext>();
} }
[Fact]
public void AddEntityFrameworkStoresCanInferKeyWithGenericBase()
{
var services = new ServiceCollection();
// This used to throw
var builder = services.AddIdentity<IdentityUser<Guid>, IdentityRole<Guid>>().AddEntityFrameworkStores<TestDbContext>();
}
} }
} }

View File

@ -37,5 +37,14 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test
// This used to throw // This used to throw
var builder = services.AddIdentity<IntUser, IntRole>().AddEntityFrameworkStores<TestDbContext>(); var builder = services.AddIdentity<IntUser, IntRole>().AddEntityFrameworkStores<TestDbContext>();
} }
[Fact]
public void AddEntityFrameworkStoresCanInferKeyWithGenericBase()
{
var services = new ServiceCollection();
// This used to throw
var builder = services.AddIdentity<IdentityUser<int>, IdentityRole<int>>().AddEntityFrameworkStores<TestDbContext>();
}
} }
} }

View File

@ -39,5 +39,13 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test
var builder = services.AddIdentity<StringUser, StringRole>().AddEntityFrameworkStores<TestDbContext>(); var builder = services.AddIdentity<StringUser, StringRole>().AddEntityFrameworkStores<TestDbContext>();
} }
[Fact]
public void AddEntityFrameworkStoresCanInferKeyWithGenericBase()
{
var services = new ServiceCollection();
// This used to throw
var builder = services.AddIdentity<IdentityUser<string>, IdentityRole<string>>().AddEntityFrameworkStores<TestDbContext>();
}
} }
} }