我在网上看到过很多类似的页面,但大多数都使用新项目而不是现有项目,或者没有必要的功能.所以,我有一个现有的MVC 5项目,并希望将ASP.NET MVC5 Identity与登录,电子邮件确认和密码重置功能集成.
除此之外,我还需要在数据库上创建所有必要的表,即用户,角色,组等(我在我的项目中使用EF Code First).是否有符合这些需求的文章或样本?任何建议将不胜感激.提前致谢...
我正在使用Entity Framework Code First和MVC 5.当我使用个人用户帐户身份验证创建我的应用程序时,我获得了一个帐户控制器以及所有必需的类和代码,以使Indiv用户帐户身份验证工作.
已经存在的代码包括:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DXContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Run Code Online (Sandbox Code Playgroud)
但后来我继续使用代码创建了我自己的上下文,所以我现在也有以下内容:
public class DXContext : DbContext
{
public DXContext() : base("DXContext")
{
}
public DbSet<ApplicationUser> Users { get; set; }
public DbSet<IdentityRole> Roles { get; set; }
public DbSet<Artist> Artists { get; set; }
public DbSet<Paintings> Paintings { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
最后,我有以下种子方法为我添加一些数据,同时开发:
protected override void Seed(DXContext context) …Run Code Online (Sandbox Code Playgroud) 我在MVC5中创建了一个类,我想要内容的主要所有者,然后我想要一些内容的编辑器:
public class Content
{
public int ID { get; set; }
public IdentityUser Owner { get; set; }
public ICollection<IdentityUser> Editors { get; set; }
public string Title{ get; set; }
public string Body { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在数据库上下文中,我有以下代码:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Content>()
.HasOptional(c => c.Editors)
.WithRequired()
.WillCascadeOnDelete();
modelBuilder.Entity<Content>()
.HasRequired(c => c.Owner)
.WithOptional()
.WillCascadeOnDelete();
}
Run Code Online (Sandbox Code Playgroud)
我希望微软以IdentityUser这种方式实现对象,它可以在其他实体类型中使用,所以我可能做错了,因为当我尝试为Content实体类型创建一个控制器时,我得到以下内容错误:
EntityType 'IdentityUserLogin' has no key defined
EntityType 'IdentityUserRole' has no key defined
EntityType: EntitySet 'IdentityUserLogins' is …Run Code Online (Sandbox Code Playgroud) 我正在尝试将配置文件/成员身份信息添加到我的MVC5应用程序中并添加配置映射.
我收到以下错误消息:
my.Models.IdentityUserLogin :: EntityType'IdentityUserLogin'没有定义键.定义此EntityType的键.
my.Models.IdentityUserRole :: EntityType'IdentityUserRole'没有定义键.定义此EntityType的键.
IdentityUserLogins:EntityType:EntitySet'IdentityUserLogins'基于没有定义键的类型'IdentityUserLogin'.
IdentityUserRoles:EntityType:EntitySet'IdentityUserRoles'基于类型'IdentityUserRole',没有定义键.
public class ApplicationUser : IdentityUser
{
public string City { get; set; }
public string Discriminator { get; set; }
public string Address { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ApplicationUserConfiguration());
}
}
Run Code Online (Sandbox Code Playgroud)