当您使用自己的 User 类进行扩展时,您必须提供 所有 Identity 使用的类型。
这是我的上下文类,注意我使用字符串作为键:
public class EarlyDetectionContext
: IdentityDbContext<User, Role, string, EDLogin, RoleUserAssociation, EDClaim>
当然,您还需要为角色 userlogin、userclaim 和 userroles 创建类。
这是我的:
public class EDClaim : IdentityUserClaim<string>
{
}
public class EDLogin : IdentityUserLogin<string>
{
}
[Table("Roles", Schema = "ED")]
public class Role : IdentityRole<string, RoleUserAssociation>
{
[Required, Column("DisplayName")]
public string DisplayName { get; set; }
[Required, Column("Created")]
public DateTime Created { get; set; }
[Required, Column("Updated")]
public DateTime Updated { get; set; }
[Required, Column("Deleted")]
public bool Deleted { get; set; }
}
[Table("RoleUserAssociations", Schema = "ED")]
public class RoleUserAssociation : IdentityUserRole<string>
{
//
// Due to Identity 2 the Id needs to of string type
//
[Key]
[Required]
public string ID { get; set; }
//[Required, Column("User_Id")]
//public User User { get; set; }
//[Required, Column("Role_Id")]
//public Role Role { get; set; }
[Required, Column("Created")]
public DateTime Created { get; set; }
[Required, Column("Updated")]
public DateTime Updated { get; set; }
[Required, Column("Deleted")]
public bool Deleted { get; set; }
}
[Table("Users", Schema = "ED")]
public class User : IdentityUser<string, EDLogin, RoleUserAssociation, EDClaim>
{
// This is needed for the new Identity 2 framework
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, string> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
[Required, Column("Municipality_Id")]
public Municipality Municipality { get; set; }
public string PasswordSalt { get; set; }
[Required, Column("FirstName")]
public string FirstName { get; set; }
[Required, Column("LastName")]
public string LastName { get; set; }
[Column("Title")]
public string Title { get; set; }
[Required, Column("Created")]
public DateTime Created { get; set; }
[Required, Column("Updated")]
public DateTime Updated { get; set; }
[Required, Column("Deleted")]
public bool Deleted { get; set; }
public bool Complete { get; set; }
[Required]
public DateTime Activated { get; set; }
}
您需要在每个类上指定键类型,在我的例子中是一个字符串,在你的例子中是一个长字符串。
另请注意,我的声明和登录类是空的。没关系,我不需要添加任何功能或属性,但我确实需要指定它们。
并且需要User类中的GenerateUserIdentityAsync方法。
在 Startup.Auth.cs 文件中需要添加:
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(EarlyDetectionContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
您还需要 OWIN 启动类(可以通过右键单击 - 添加 - 新建项目来添加)和 Identity.cs 文件。
如果您能阅读德语,请查看blog
它基本上描述了你想要做什么。 (虽然采用 db-first 方法)
也看他的video(他的英语说得很好)。
还有part 2 也是。
我希望这可以为您提供足够的信息来继续前进:)