C# 从IdentityUser继承我在UserManager上收到一个错误<;用户>;
我正在用.NET Framework 4.5.1和Asp.NET Identity 2.1.0开发一个Web Api 2.2应用程序 我不确定我在做什么,但我想将我的数据库与ASP.NET Identity数据库合并,我已完成以下操作: 我自己的C# 从IdentityUser继承我在UserManager上收到一个错误<;用户>;,c#,asp.net,entity-framework,asp.net-identity,C#,Asp.net,Entity Framework,Asp.net Identity,我正在用.NET Framework 4.5.1和Asp.NET Identity 2.1.0开发一个Web Api 2.2应用程序 我不确定我在做什么,但我想将我的数据库与ASP.NET Identity数据库合并,我已完成以下操作: 我自己的dbContext public class EFDbContext : IdentityDbContext, IUnitOfWork 我自己的用户类 public class User : IdentityUser<long,
dbContext
public class EFDbContext : IdentityDbContext, IUnitOfWork
我自己的用户
类
public class User :
IdentityUser<long,
IdentityUserLogin<long>,
IdentityUserRole<long>,
IdentityUserClaim<long>
>
这里有三个错误:
EFDbContext_ctx = context as EFDbContext;
_userManager = new UserManager<User, long>(new UserStore<User>(_ctx));
EFDbContext\u ctx=context作为EFDbContext;
_userManager=newusermanager(newuserstore(_ctx));
UserManager\u UserManager;
您使用的是:
表示用户的主键为字符串类型的用户的用户管理器
当您使用自己的用户类进行扩展时,您必须提供Identity使用的所有类型 这是我的上下文类,请注意,我使用字符串作为键:
public class EarlyDetectionContext
: IdentityDbContext<User, Role, string, EDLogin, RoleUserAssociation, EDClaim>
public类早期检测上下文
:IdentityDbContext
当然,您还需要为角色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; }
}
公共类EDClaim:IdentityUserClaim
{
}
公共类EDLogin:IdentityUserLogin
{
}
[表(“角色”,Schema=“ED”)]
公共类角色:IdentityRole
{
[必填,列(“显示名称”)]
公共字符串DisplayName{get;set;}
[必填,列(“已创建”)]
已创建公共日期时间{get;set;}
[必填,第列(“更新”)]
公共日期时间已更新{get;set;}
[必填,第列(“已删除”)]
公共bool已删除{get;set;}
}
[表(“RoleUserAssociations”,Schema=“ED”)]
公共类角色关联:IdentityUserRole
{
//
//由于标识2,Id需要为字符串类型
//
[关键]
[必需]
公共字符串ID{get;set;}
//[必填,第列(“用户Id”)]
//公共用户{get;set;}
//[必填,列(“角色Id”)]
//公共角色{get;set;}
[必填,列(“已创建”)]
已创建公共日期时间{get;set;}
[必填,第列(“更新”)]
公共日期时间已更新{get;set;}
[必填,第列(“已删除”)]
公共bool已删除{get;set;}
}
[表(“用户”,Schema=“ED”)]
公共类用户:IdentityUser
{
//这是新的Identity 2框架所需要的
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
[必填,第列(“市镇Id”)]
公共自治市{get;set;}
公共字符串密码salt{get;set;}
[必填,第列(“名字”)]
公共字符串名{get;set;}
[必填,第列(“姓氏”)]
公共字符串LastName{get;set;}
[栏目(“标题”)]
公共字符串标题{get;set;}
[必填,列(“已创建”)]
已创建公共日期时间{get;set;}
[必填,第列(“更新”)]
公共日期时间已更新{get;set;}
[必填,第列(“已删除”)]
公共bool已删除{get;set;}
公共布尔完成{get;set;}
[必需]
已激活公共日期时间{get;set;}
}
您需要在每个类上指定键类型,在我的例子中是字符串,在您的例子中是long。
还要注意,我的claim和login类是空的。没关系,我不需要在我的案例中添加任何功能或属性,但我需要指定它们
并且需要用户类中的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);
//将数据库上下文、用户管理器和登录管理器配置为每个请求使用一个实例
app.CreatePerOwinContext(EarlyDetectionContext.Create);
app.CreatePerOwinContext
它基本上描述了你要做的事情。(但采用db优先的方法)
也要看他的(他英语说得很好)。
而且它也是
我希望这能为您提供足够的信息,以便继续前进:)谢谢您的回答。现在,我在这里遇到了同样的问题:\u userManager=newusermanager(newuserstore(_ctx))代码>。您的用户存储应该将您的上下文作为其类型参数。我有一些类似的东西。您是否通过实例化UserManager
修复了最后一个错误?请看我的问题
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; }
}
// 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);