【发布时间】:2018-09-22 14:29:40
【问题描述】:
我已按照this 文章将身份类的主键数据类型从 String 修改为 Guid,它确实为 SQL Server 中的各个列创建了 uniqueidentifier 数据类型,而不是默认的 NVARCHAR 但我我无法使用 UserManager 和 SignInManager 类,因为它们拒绝接受自定义的 ApplicationUser 类(如文章中所述扩展 IdentityUser)而不是默认的 IdentityUser 类。
本文没有详细介绍,而是通过查看 SO 上其他类似问题的答案
就像 this one 一样,我似乎要么必须更改 Identity 的所有类才能实现这一目标,要么使用我不完全确定如何使用的“其他”UserManager 类。
我的自定义身份模型如下所示:
public class IdentityModels
{
// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser<Guid>
{
}
public class ApplicationRole : IdentityRole<Guid>
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(GetConnectionString());
}
private static string GetConnectionString()
{
const string databaseName = "testname";
const string databaseUser = "testuser";
const string databasePass = "testpass";
return $"Server=localhost;" +
$"database={databaseName};" +
$"uid={databaseUser};" +
$"pwd={databasePass};" +
$"pooling=true;";
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Core Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Core Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
}
在 Identity 2.0 中更改身份类的主键属性的数据类型以便UserManager 和SignInManager 接受修改的正确方法是什么?
【问题讨论】:
标签: c# asp.net asp.net-identity