>我覆盖了身份类:
[Table("Roles")]
public partial class AppRole : IdentityRole<int, AppUserRole, AppRoleClaim>
{
}
[Table("RoleClaims")]
public partial class AppRoleClaim : IdentityRoleClaim<int>
{
}
[Table("Users")]
public partial class AppUser : IdentityUser<int, AppUserClaim, AppUserRole, AppUserLogin>
{
}
[Table("UserClaims")]
public partial class AppUserClaim : IdentityUserClaim<int>
{
}
[Table("UserLogins")]
public partial class AppUserLogin : IdentityUserLogin<int>
{
[Key]
[Required]
public int Id { get; set; }
}
[Table("UserRoles")]
public partial class AppUserRole : IdentityUserRole<int>
{
}
[Table("UserTokens")]
public partial class AppUserToken : IdentityUserToken<int>
{
[Key]
[Required]
public int Id { get; set; }
}
public class AppRoleManager : RoleManager<AppRole>
{
public AppRoleManager(IRoleStore<AppRole> store, IEnumerable<IRoleValidator<AppRole>> roleValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, ILogger<RoleManager<AppRole>> logger, IHttpContextAccessor contextAccessor) : base(store, roleValidators, keyNormalizer, errors, logger, contextAccessor)
{
}
}
public class AppRoleStore : RoleStore<AppRole, AppDbContext, int, AppUserRole, AppRoleClaim>
{
public AppRoleStore(AppDbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
{
}
protected override AppRoleClaim CreateRoleClaim(AppRole role, Claim claim)
{
return new AppRoleClaim(role, claim);
}
}
public class AppSignInManager : SignInManager<AppUser>
{
public AppSignInManager(UserManager<AppUser> userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<AppUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<AppUser>> logger) : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger)
{
}
}
public class AppUserManager : UserManager<AppUser>
{
public AppUserManager(IUserStore<AppUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<AppUser> passwordHasher, IEnumerable<IUserValidator<AppUser>> userValidators, IEnumerable<IPasswordValidator<AppUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<AppUser>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
{
var LoginStore = Store;
}
}
public class AppUserStore : UserStore<AppUser, AppRole, AppDbContext, int, AppUserClaim, AppUserRole, AppUserLogin, AppUserToken, AppRoleClaim>
{
public AppUserStore(AppDbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
{
}
protected override AppUserClaim CreateUserClaim(AppUser user, Claim claim)
{
return new AppUserClaim(user, claim);
}
protected override AppUserLogin CreateUserLogin(AppUser user, UserLoginInfo login)
{
return new AppUserLogin(user, login);
}
protected override AppUserRole CreateUserRole(AppUser user, AppRole role)
{
return new AppUserRole(user, role);
}
protected override AppUserToken CreateUserToken(AppUser user, string loginProvider, string name, string value)
{
return new AppUserToken(user, loginProvider, name, value);
}
}
我想为谷歌使用外部登录,但是当我打电话给await UserManager.FindByLoginAsync(info.LoginProvider, info.ProviderKey);
时出现错误:
实体类型"AppUserLogin"是使用单个键属性定义的,但 2 个值被传递给 'DbSet.Find' 方法。
我正在遵循教程 openiddict 和 ASP.NET 核心和 Angular 2,但我很确定这个错误与这 2 个教程没有任何关系。我认为我的代码不起作用,因为我更改了用户登录表的 TKey 表:public partial class AppUserLogin : IdentityUserLogin<int>
。即使使用单个键属性定义搜索也不会产生任何结果。
解决方案在DbContext.OnModelCreating
方法中。我从:
modelBuilder.Entity<AppUserLogin>(entity =>
{
entity
.HasKey(u => u.Id);
entity.Property(p => p.Id)
.ValueGeneratedOnAdd();
});
自:
modelBuilder.Entity<AppUserLogin>(entity =>
{
entity
.HasKey(u => u.Id);
entity.Property(p => p.Id)
.ValueGeneratedOnAdd();
entity
.HasKey(u => new { u.LoginProvider, u.ProviderKey });
});
它有效。