From 1a0cd3c4d15be988abefd889853b0c6d973db9f4 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Mon, 8 Aug 2016 13:21:20 -0700 Subject: [PATCH] Add generic parm for IdentityRoleClaim --- .../UserStore.cs | 43 ++++++++++++++++--- .../UserStoreWithGenericsTest.cs | 29 ++++++++++--- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/UserStore.cs b/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/UserStore.cs index 151e462e1a..22cc54bbe5 100644 --- a/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/UserStore.cs +++ b/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/UserStore.cs @@ -69,10 +69,10 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore /// The type of the data context class used to access the store. /// The type of the primary key for a role. public class UserStore : UserStore, IdentityUserRole, IdentityUserLogin, IdentityUserToken> - where TUser : IdentityUser - where TRole : IdentityRole - where TContext : DbContext - where TKey : IEquatable + where TUser : IdentityUser + where TRole : IdentityRole + where TContext : DbContext + where TKey : IEquatable { /// /// Constructs a new instance of . @@ -158,6 +158,38 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore /// The type representing a user external login. /// The type representing a user token. public abstract class UserStore : + UserStore> + where TUser : IdentityUser + where TRole : IdentityRole> + where TContext : DbContext + where TKey : IEquatable + where TUserClaim : IdentityUserClaim + where TUserRole : IdentityUserRole + where TUserLogin : IdentityUserLogin + where TUserToken : IdentityUserToken + { + /// + /// Creates a new instance of . + /// + /// The context used to access the store. + /// The used to describe store errors. + public UserStore(TContext context, IdentityErrorDescriber describer = null) : base(context, describer) { } + } + + + /// + /// Represents a new instance of a persistence store for the specified user and role types. + /// + /// The type representing a user. + /// The type representing a role. + /// The type of the data context class used to access the store. + /// The type of the primary key for a role. + /// The type representing a claim. + /// The type representing a user role. + /// The type representing a user external login. + /// The type representing a user token. + /// The type representing a role claim. + public abstract class UserStore : IUserLoginStore, IUserRoleStore, IUserClaimStore, @@ -170,13 +202,14 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore IUserTwoFactorStore, IUserAuthenticationTokenStore where TUser : IdentityUser - where TRole : IdentityRole> + where TRole : IdentityRole where TContext : DbContext where TKey : IEquatable where TUserClaim : IdentityUserClaim where TUserRole : IdentityUserRole where TUserLogin : IdentityUserLogin where TUserToken : IdentityUserToken + where TRoleClaim : IdentityRoleClaim { /// /// Creates a new instance of . diff --git a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/UserStoreWithGenericsTest.cs b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/UserStoreWithGenericsTest.cs index 0e9b53a20b..a1f2c9913d 100644 --- a/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/UserStoreWithGenericsTest.cs +++ b/test/Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test/UserStoreWithGenericsTest.cs @@ -199,7 +199,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test } } - public class UserStoreWithGenerics : UserStore + public class UserStoreWithGenerics : UserStore { public string LoginContext { get; set; } @@ -248,7 +248,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test } } - public class RoleStoreWithGenerics : RoleStore> + public class RoleStoreWithGenerics : RoleStore { private string _loginContext; public RoleStoreWithGenerics(ContextWithGenerics context, string loginContext) : base(context) @@ -256,9 +256,9 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test _loginContext = loginContext; } - protected override IdentityRoleClaim CreateRoleClaim(MyIdentityRole role, Claim claim) + protected override IdentityRoleClaimWithIssuer CreateRoleClaim(MyIdentityRole role, Claim claim) { - return new IdentityRoleClaim { RoleId = role.Id, ClaimType = claim.Type, ClaimValue = claim.Value }; + return new IdentityRoleClaimWithIssuer { RoleId = role.Id, ClaimType = claim.Type, ClaimValue = claim.Value, Issuer = claim.Issuer }; } } @@ -279,12 +279,29 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test } } + public class IdentityRoleClaimWithIssuer : IdentityRoleClaim + { + public string Issuer { get; set; } + + public override Claim ToClaim() + { + return new Claim(ClaimType, ClaimValue, null, Issuer); + } + + public override void InitializeFromClaim(Claim other) + { + ClaimValue = other.Value; + ClaimType = other.Type; + Issuer = other.Issuer; + } + } + public class IdentityUserRoleWithDate : IdentityUserRole { public DateTime Created { get; set; } } - public class MyIdentityRole : IdentityRole> + public class MyIdentityRole : IdentityRole { public MyIdentityRole() : base() { @@ -307,7 +324,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test public string Context { get; set; } } - public class ContextWithGenerics : IdentityDbContext, IdentityUserTokenWithStuff> + public class ContextWithGenerics : IdentityDbContext { public ContextWithGenerics(DbContextOptions options) : base(options) { } }