ASP NET MVC Identity 2.0 Sample
-
Microsoft.AspNet.Identity.Owin
-
Microsoft.AspNet.Identity.EntityFramework
-
Microsoft.Owin.Host.SystemWeb
namespace AspNetIdentity.Data
{
public class AppDbContext : IdentityDbContext
{
}
}
不客製跳至 Migration DataBase
namespace AspNetIdentity.Data
{
public class AppDbContext : IdentityDbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("AppUser");
modelBuilder.Entity<IdentityRole>().ToTable("AppRole");
modelBuilder.Entity<IdentityUserClaim>().ToTable("AppUserClaim");
modelBuilder.Entity<IdentityUserLogin>().ToTable("AppUserLogin");
modelBuilder.Entity<IdentityUserRole>().ToTable("AppUserRole");
}
}
}
新增 AppUser 並繼承 IdentityUser
namespace AspNetIdentity.Data.Entity
{
public class AppUser : IdentityUser
{
public string Name { get; set; }
public bool IsDeleted { get; set; }
public DateTime CreateDateTime { get; set; }
}
}
修改 DbContext 繼承 IdentityDbContext 並修改 IdentityUser 為 AppUser
namespace AspNetIdentity.Data
{
public class AppDbContext : IdentityDbContext<AppUser>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AppUser>().Property(x => x.Name).HasMaxLength(256).IsRequired().IsUnicode(true);
modelBuilder.Entity<AppUser>().Property(x => x.IsDeleted).IsRequired();
modelBuilder.Entity<AppUser>().Property(x => x.CreateDateTime).IsRequired();
//...
}
}
}
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=AspNetIdentity;Trusted_Connection=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
- Enable-Migrations
- Add-Migration init
- Upage-Database
App_Start 資料夾下新增項目,OWIN 啟動類別,命名Startup.cs
新增 class AppBuilderExtensions ,設定配置 code