链接ASP.NET标识用户到用户详细信息表
本文关键字:用户 详细信息 标识 ASP NET 链接 | 更新日期: 2023-09-27 18:18:08
我使用了带有个人授权的默认MVC模板。运行应用程序后,它会自动创建所需的Identity表。我已经成功注册了几个用户,它们都存储在AspNetUser
表中。
现在我想要的是登录用户可以添加更多的信息,如名字,姓氏,地址等。为此,我创建了另一个表PersonalInformation
,其中包含用于存储这些信息的列。
我还在ManageController
中创建了EditProfile
动作。
现在我如何链接ASP。. NET身份用户到该表?我应该添加Id
列作为AspNetUser
的外键吗?另外,我怎样才能成功地将编辑过的信息与登录的用户id一起存储在"PersonalInformation"表中?
您可以在用户和个人信息之间创建一对一的关系,然后使用应用程序用户管理器创建或更新用户。
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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;
}
public virtual PersonalInformation PersonalInformation { get; set; }
}
public class PersonalInformation
{
[Key, ForeignKey("User")]
public string UserId { get;set; }
public string FirstName { get; set; }
// other fields...
public virtual ApplicationUser User { get;set; }
}
// Create user
var store = new UserStore<ApplicationUser>(context);
var manager = new ApplicationUserManager(store);
var user = new ApplicationUser() { Email = "email@email.com", UserName = "username", PersonalInformation = new PersonalInformation { FirstName = "FirstName" } };
manager.Create(user, "Password123!");
// Update user
var store = new UserStore<ApplicationUser>(context);
var manager = new ApplicationUserManager(store);
var user = manager.Users.FirstOrDefault(u => u.Id == id);
user.PersonalInformation.FirstName = "EditedName";
manager.Update(user);
标识与数据库的交互方式是通过实体框架自动实现的。在您的解决方案中应该有一个名为ApplicationDbContext的类,这是实体框架上下文。就我个人而言,我建议查找一些实体框架指南,并找出它如何与身份交互。但一个快速的概述是:
要使用实体框架向数据库中添加另一个表,您需要在上下文中添加DbSet。
public DbSet<PersonalInformation> personalInformation {get;set;}
然后需要更新ApplicationUser以保存对personalinformation的引用。然后需要将数据库迁移到最新版本,可以使用生成的迁移,也可以使用自动迁移。
是的,您应该在表中添加一个外键。如何做到这一点取决于您如何设置实体框架(EDMX/代码优先/从数据库逆向工程),但这个过程在许多其他问题中都有解释。这是一个单独的问题,以前已经回答过了,试着搜索一下。
现在要存储用户的详细信息,您可以在注册该用户时在PersonalInformation
表中创建一条记录,或者在编辑用户时检查它是否存在:
var currentUserId = User.Identity.GetUserId();
// Look up existing record
var personalInformation = _dbContext.PersonalInformation
.FirstOrDefault(p => p.UserId == currentUserId);
if (personalInformation == null)
{
// If not exists, create and attach new record
personalInformation = _dbContext.PersonalInformation.Create();
}
// Map incoming model properties to entity
personalInformation.FirstName = model.FirstName;
personalInformation.Address = model.Address;
// ...
// Add or update the entity in the database
_dbContext.SaveChanges();
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var ctx = store.Context;
var currentUser = manager.FindById(User.Identity.GetUserId());
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var ctx = store.Context;
var currentUser = manager.FindById(User.Identity.GetUserId());
pedido.Nombre = currentUser.Nombre;