I\'ve used default MVC template with individual authorization. After running the application it automatically creates the required Identity tables. I\'ve successfully regist
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);
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.
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();
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;
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());