Link ASP.NET Identity users to user detail table

后端 未结 5 2074
旧巷少年郎
旧巷少年郎 2020-12-29 16:46

I\'ve used default MVC template with individual authorization. After running the application it automatically creates the required Identity tables. I\'ve successfully regist

相关标签:
5条回答
  • 2020-12-29 17:03

    You can create one to one relationship between user and personal information and then create or update user using Application User Manager.

    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);
    
    0 讨论(0)
  • 2020-12-29 17:08

    The way identity interfaces with the database is automatic via the entity framework. In your solution there should be a class called ApplicationDbContext, this is the entity framework context. Personally i would suggest looking up some entity framework guides and work out how it interacts with identity. but a quick overview would be :

    To add another table to your database with entity framework you would need to add a DbSet into the context.

    public DbSet<PersonalInformation> personalInformation {get;set;}
    

    Then you would need to update the ApplicationUser to hold a reference to a personalInforamtion. Then you would need to migrate the database to the latest version, either with a generated migration or with automatic migrations.

    Any changes you would then do would be through the entity framework.

    0 讨论(0)
  • 2020-12-29 17:08

    Yes, you should add a foreign key to your table. How to do so depends on how you set up your Entity Framework (EDMX / code first / reverse engineered from database), but this proceduree is explained in plenty other questions. That is a separate question and that has been answered before, try searching.

    Now to store the details for a user, you can either create a record in your PersonalInformation table upon registering this user, or checking if it exists when editing a user:

    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();
    
    0 讨论(0)
  • 2020-12-29 17:13
    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;
    
    0 讨论(0)
  • 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());
    
    0 讨论(0)
提交回复
热议问题