0

I want to use ASP.Net-Identity for user management. For this, I want to extend the IdentityUser class with a few attributes.

public class AppUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<AppUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    } 

    public int Settings_ID { get; set; }
    public string Position { get; set; }
    public string CompanyName { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
[...]

Now, in my data context, I want to create just one table for this user model:

 public class AntContext : IdentityDbContext<AppUser>
{

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer<AntContext>(null);
        modelBuilder.Entity<AppUser>().ToTable("AppUsers");
        base.OnModelCreating(modelBuilder);
    }
        public override IDbSet<AppUser> Users { get; set; }
[...]

However, when trying to update-database, I receive the error:

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.AppUsers_dbo.AspNetUsers_Id". The conflict occurred in database "C:\USERS\NEUMA\SOURCE\REPOS\MYANTON\MYANTON\APP_DATA\ASPNETDB.MDF", table "dbo.AspNetUsers", column 'Id'.

I understand that AspNetUsers is a table from IdentityUser, but how do I create only one table so that there are no conflicts?

How do I properly extend the IdentityUserclass and use it in my data context?

Cœur
  • 37,241
  • 25
  • 195
  • 267
mneumann
  • 713
  • 2
  • 9
  • 42

1 Answers1

1

The solution is to not mix contexts. Maintain a seperation of concerns. Use the migration script for Identity and create a new script for the business context (your data context).

Add a Users table to the business context that references the name or sub from the IdentityContext. Please read my answer here for a quite similar question.

Also, there is no need to extend the ApplicationUser table. You can use AspNetUserClaims to add this kind of information. With a custom type:

new Claim("http://www.myapp.com/Firstname", "Firstname");

Or an existing WIF ClaimType:

new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname", "Lastname");

As for CompanyName, this may actually be part of the business context.

  • I accepted your answer as it seems logical to me. If I understand you correctly, I create a `Users` table with my business logic and leave the `IdentityUser` as it is. How would my `User` be referenced to their respective `Identity` - just by referencing the `Id`? – mneumann Oct 05 '18 at 11:02
  • Yes, the value of AspNetUsers.Id should be available in a claim (sub, userid). You can use that to reference the user. This way you can link the current user to the DataContext.User (as foreign key), where DataContext.User.Id can be an automatic number and used for DataContext relations to the user. –  Oct 05 '18 at 11:14