将 ASP.NET 身份实施到现有数据库中
Implement ASP.NET Identity into an existing database
我有一个现有项目和 SQL 数据库,其中包含一个用户 table(我们称它为“"MyOldUsersTable"”)并附上 tables(地址、电话、职位, 等) 具有 PK-FK 关系。
注意:此数据库既不使用成员资格也不使用身份。它是从另一个项目中提取的数据库,"MyOldUsersTable" 不包含任何登录信息(密码、上次登录等...)
我开始使用具有标识的 Web Api 项目,但它生成默认值 tables(AspNetUsers、AspNetUserRoles、AspNetUserLogins...)
目标:获得ASP.NET身份以使用现有的tables
我试过这个解决方案:
How can I change the table names when using Visual Studio 2013 ASP.NET Identity?
但是身份将 "MyOldUsersTable" 重命名为 "MyOldUsersTable1" 并将 "AspNetUsers" table 重命名为 "MyOldUsersTable"。那不是很有用。
最好的方法是什么?我看到三个选项:
- 解决方案 1:将 "MyOldUsersTable" table 字段移动到 "ASPNETUser" table 并将 PK-FK 更改为其他 tables
- 解决方案 2:让身份了解用户 table 现在是 "MyOldUsersTable"
- 方案三:保留两个系统,只在"AspNetUsers"和"MyOldUsersTable"
之间加一个PK-FK
方案二:保留两个系统,在"AspNetUsers"和"MyOldUsersTable"之间加一个PK-FK即可。可能,但字段中可能有重复项(电子邮件,phone)。还需要维护两个用户 table 并不理想。
在 table 之间添加一对零对一的关系时有效,如下所示:
modelBuilder.Entity<MyOldUsersTable>()
.HasOptional(e => e.ApplicationUser)
.WithRequired(e => e.MyOldUsersTable)
.Map(m => m.MapKey("MyOldUsersTableId"))
.WillCascadeOnDelete(false);
最佳:解决方案 3. 通过删除来自 IdentityUser 的所有默认行为,让 Identity 明白用户 table 现在是 "MyOldUsersTable"。
第一步是通过替换删除对 IdentityDbContext 的引用:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
作者:
public class ApplicationDbContext : DbContext
然后将默认的 UserStore 替换为自定义的:
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
替换为:
var manager = new ApplicationUserManager(new CustomUserStore(context.Get<ApplicationDbContext>()));
最后,使用所需的接口实现了 CustomUserStore。例如密码、电子邮件和安全戳:
public class CustomUserStore : IUserStore<MyOldUsersTable, int>, IUserPasswordStore<MyOldUsersTable, int>, IUserEmailStore<MyOldUsersTable, int>, IUserSecurityStampStore<MyOldUsersTable, int>
我有一个现有项目和 SQL 数据库,其中包含一个用户 table(我们称它为“"MyOldUsersTable"”)并附上 tables(地址、电话、职位, 等) 具有 PK-FK 关系。
注意:此数据库既不使用成员资格也不使用身份。它是从另一个项目中提取的数据库,"MyOldUsersTable" 不包含任何登录信息(密码、上次登录等...)
我开始使用具有标识的 Web Api 项目,但它生成默认值 tables(AspNetUsers、AspNetUserRoles、AspNetUserLogins...)
目标:获得ASP.NET身份以使用现有的tables
我试过这个解决方案: How can I change the table names when using Visual Studio 2013 ASP.NET Identity? 但是身份将 "MyOldUsersTable" 重命名为 "MyOldUsersTable1" 并将 "AspNetUsers" table 重命名为 "MyOldUsersTable"。那不是很有用。
最好的方法是什么?我看到三个选项:
- 解决方案 1:将 "MyOldUsersTable" table 字段移动到 "ASPNETUser" table 并将 PK-FK 更改为其他 tables
- 解决方案 2:让身份了解用户 table 现在是 "MyOldUsersTable"
- 方案三:保留两个系统,只在"AspNetUsers"和"MyOldUsersTable" 之间加一个PK-FK
方案二:保留两个系统,在"AspNetUsers"和"MyOldUsersTable"之间加一个PK-FK即可。可能,但字段中可能有重复项(电子邮件,phone)。还需要维护两个用户 table 并不理想。
在 table 之间添加一对零对一的关系时有效,如下所示:
modelBuilder.Entity<MyOldUsersTable>()
.HasOptional(e => e.ApplicationUser)
.WithRequired(e => e.MyOldUsersTable)
.Map(m => m.MapKey("MyOldUsersTableId"))
.WillCascadeOnDelete(false);
最佳:解决方案 3. 通过删除来自 IdentityUser 的所有默认行为,让 Identity 明白用户 table 现在是 "MyOldUsersTable"。
第一步是通过替换删除对 IdentityDbContext 的引用:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
作者:
public class ApplicationDbContext : DbContext
然后将默认的 UserStore 替换为自定义的:
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
替换为:
var manager = new ApplicationUserManager(new CustomUserStore(context.Get<ApplicationDbContext>()));
最后,使用所需的接口实现了 CustomUserStore。例如密码、电子邮件和安全戳:
public class CustomUserStore : IUserStore<MyOldUsersTable, int>, IUserPasswordStore<MyOldUsersTable, int>, IUserEmailStore<MyOldUsersTable, int>, IUserSecurityStampStore<MyOldUsersTable, int>